Search array values in string in php

If we have to search an array values in string two possible situations arise.

1. The string contains all array values.
2. The string contains at least one array value.

Here are two little functions that searches for an array value in string.

At least one array value exists in string

  function array_in_string($string, $array) {
    foreach ($array as $value) {
        $status=stristr($string, $value);
        if ($status) {
            return true;
        }
    }
    return false;
  }
 

Example of above function

$text=array('text', 'search');
$string='This is the search string';

$match=array_in_string($string,$text);

if($match){
echo 'Found';
}else{
echo 'Not found';
}

All array values exist in string

  function array_in_string($string, $array) {

    foreach ($array as $value) {
        $status=stristr($string, $value);
        if (!$status) {
            return false;
            }
    }
    return true;
  }
 

Example of above function

$text=array('text', 'search');
$string='This is the search string';

$match=array_in_string($string,$text);

if($match){
echo 'Found';
}else{
echo 'Not found';
}
Search array values in string in php Search array values in string in php Reviewed by JS Pixels on March 22, 2012 Rating: 5

No comments:

Altaf Web. Powered by Blogger.