If you need to use the string. You will have to split it into an array first using the explode method. However you will have to remove or drop the last ; in your string before using the explode method. If you do not you will get an empty delimiter error for your stristr or strstr function calls for the last index of the array.
<?php
$var = "Josh" . ";" . "Chloe" . ";" . "Marie" . ";" . "John";
//split $var into an array(Remove the last ; or you will get an error)
//Warning: stristr() [function.stristr]: Empty delimiter in C:\wamp\www\toolbox\test.php on line 17
$namesArray = explode(";",$var);
$text2 = "marie is beautiful"; //marie is in text2, so I want to call a function.
//loop through the names array
foreach($namesArray as $name){
if(stristr($text2,$name) != false){
doSomething();
}
}
function doSomething(){
echo "do something";
}
?>
If you want a more reusable version then I would build a function something like the following:
<?php
$var = "Josh" . ";" . "Chloe" . ";" . "Marie" . ";" . "John";
//split $var into an array(Remove the last ; or you will get an error)
//Warning: stristr() [function.stristr]: Empty delimiter in C:\wamp\www\toolbox\test.php on line 17
$namesArray = explode(";",$var);
//the input text to use as a test
$input = "marie is beautiful"; //marie is in text2, so I want to call a function.
//call the containsName function
containsName($input, $namesArray,"doSomething");
function containsName($input,$namesArray,$function){//begin function
//loop through the names array
foreach($namesArray as $name){
//if the array index contains the name in the input variable
if(stristr($input,$name) != false){
//call the function given by the $function parameter
$function();
}
}
}//end function
function doSomething(){
echo "do something";
}
?>