I have a string like abcdefg123hijklm. I also have an array which contains several strings. Now I want to check my abcdefg123hijklm and see if the 123 from abcdefg123hijklm is in the array. How can I do that? I guess in_array() wont work?

Thanks?

share|improve this question

1 Answer

up vote 4 down vote accepted

So you want to check if any substring of that particular string (lets call it $searchstring) is in the array? If so you will need to iterate over the array and check for the substring:

foreach($array as $string)
{
  if(strpos($searchstring, $string) !== false) 
  {
    echo 'yes its in here';
    break;
  }
}

See: http://php.net/manual/en/function.strpos.php

If you want to check if a particular part of the String is in the array you will need to use substr() to separate that part of the string and then use in_array() to find it.

http://php.net/manual/en/function.substr.php

share|improve this answer
2  
I would put the echo part in a code block (curly brackets) and add a break; right before the closing curly bracket - helps in performance when you just need to know if at least one string matches. – Christian Jan 17 '12 at 9:17
2  
^ Ack and implemented :) – bardiir Jan 17 '12 at 9:25

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.