-1

Is there a way to detect if a variable is a pattern in PHP? I know this is possible in JavaScript since patterns are a variable type however in PHP, patterns are inside of strings.

if (is_pattern($myPattern)) {
        //...
}
1
  • You need to use preg_match() for that. If I understood correctly what you want.
    – some_guy
    Commented Aug 7, 2020 at 21:02

1 Answer 1

0

In PHP there is a function called preg_match that basically does what you stated, it tests a string in search of a pattern/regex.

if (preg_match("/php/i", "php is the best programming language.")) {
    echo "A match has been found!";
} else {
    echo "No match found.";
}

In that case, preg_match would search for the string "php" inside the test string that you have passed as an argument.

1
  • You should (re)read the question! They want to know if a string is a pattern.
    – Toto
    Commented Aug 8, 2020 at 8:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.