tags:

views:

35

answers:

3

i am searchgin for a function in php, or a very lightweight and short/easy way to find out if a set (array) of characters appear in a given array

if(chars_in_string(array("x","d","9", "ü"), $anystring) ) do_something()

while x,d,9, ü are just as an example ... hmmm maybe i can solve this with regular expressions?

+4  A: 
function all_chars_in_string($chars,$string) {
   foreach($chars as $char) {
      if (strpos($string,$char) === false) return false;
   }
   return true;
}

function any_chars_in_string($chars,$string) {
   foreach($chars as $char) {
      if (strpos($string,$char) !== false) return true;
   }
   return false;
}
Mark Baker
:-) i thought of an easier way ...
helle
If you have an easier way, then show it for the benefit of others
Mark Baker
A: 

Looking if any char is in string

function anyCharInString($chars,$string) {
   foreach($chars as $chr)
      if (strpos($string,$chr) !== false) return true;
   return false;
}

Look if all chars are in the string

function allCharsInString($chars,$string) {
   foreach($chars as $chr)
      if (strpos($string,$chr) === false) return false;
   return true;
}

EDIT: I'm to slow.

Hippo
+5  A: 

Just use strpbrk($string, implode(array("x","d","9", "ü"))); :)

Blizz
+1 Forgot about that function
Mark Baker
It just so happens I looked it up again recently because I needed it as well... It has a so-easy-to-remember name ;)
Blizz
this is what i was looking for! thanks very much ;-) and really easy to remember.
helle