Question

I'm looking for some wildcards to the strpos function similar to those used in preg_replace or preg_match but I can't find any, here is an idea:

<?php
if (strpos("The black\t\t\thorse", "black horse") === false)
  echo "Text NOT found.";
else
  echo "Text found.";
?>

Here the result will be: Text NOT found.

Now I want to use one wildcard to omit spaces or horizontal tab like below:

<?php
if (strpos("The black\t\t\thorse", "black/*HERE THE WILDCARD*/horse") === false)
  echo "Text NOT found.";
else
  echo "Text found.";
?>

And here the idea is that the result is: Text found.

Does anyone know something about ?

Was it helpful?

Solution

strpos() doesn't match patterns, if you want to match patterns you have to use preg_match() this should work for your situation.

<?php
    if (preg_match('/black[\s]+horse/', "The black\t\t\thorse"))
      echo "Text found.";
    else
      echo "Text not found.";
?>

OTHER TIPS

If you need the first occurrence of the match then you can use the PREG_OFFSET_CAPTURE flag:

preg_match('/black\shorse/i', "The black\t\t\thorse", $matches, PREG_OFFSET_CAPTURE);
var_dump($matches);

will result in

array(1) { [0]=> array(2) { [0]=> string(13) "black horse" [1]=> int(4) } }

where $matches[0][1] is your position

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top