Find if substring exists in string

I have many lines, and I need to check if each of them contains color.

For instance:

  • Bird in the sky
  • 22nd street of France
  • The dog is blue.
  • Cat black and white

So, the last two lines should return true.

What is the best way to find it?

Regex or check with any substr ()?

+5
source share
4 answers

In regexp you can write

preg_match_all("/(red|blue|black|white|etc)/", $haystack, $matches);

print_r($matches);

Use a loop for all lines, and you will easily notice which of the values ​​matches you $.

+8
source

I always work with strpos, as this seems to be the fastest alternative (although I don't know about regex).

if(strpos($haystack, $needle) !== FALSE) return $haystack;
+23
source

strpos, , 1,2,3 .., true false.

, , 0, , strpos .

+3
source

strpos or strripos in php should be able to search for a single word in a string. You may need a loop to search for all colors if you use it, though

+1
source

All Articles