Find if substring exists in a string

bahamut100 picture bahamut100 · Aug 4, 2011 · Viewed 29.9k times · Source

I have a lot of strings, and I need to check if each of them contains a color.

For example :

  • A bird in the sky
  • 22 street of France
  • The dog is blue
  • The cat is black and white

So, the two last strings must return true.

What is the best way to find it?

Regex, or check with any substr() ?

Answer

Quasdunk picture Quasdunk · Aug 4, 2011

I always work with strpos since it seems to be the fastest alternative (don't know about regex though).

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