Forum Moderators: coopster

Message Too Old, No Replies

Search string using an array of values

         

gosman

8:43 pm on Mar 12, 2010 (gmt 0)

10+ Year Member



I have the following code

$string="red and blue are primary colors";
$colors=array('red','green','blue');
$colorvalue=array();

I want to acheive the following.

1. Search the $string to find any occurrence of the values in the $colors array

2. when a match is found populate the $colorvalue array with the color key.

So using the above example I would have the following result

$colorvalue=array(0,2);

Any help really appreciated.

Anyango

9:28 am on Mar 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<?
$string="red and blue are primary colors";
$colors=array('red','green','blue');
$colorvalue=array();

$i=0;
foreach($colors as $color)
{
if(strpos($string,$color)!==false)
{
$colorvalue[]=$i;
}
$i++;
}

print_r($colorvalue);
?>

gosman

8:16 pm on Mar 14, 2010 (gmt 0)

10+ Year Member



Thank you Anyango works great.