Q:

how to find position after search word into string then mark this

I'm going to find desired word in a string, and then I show Mark or highlight
$str = "this is a test for testing a test function";

$array = explode(' ',$str);
$key = array_search('tes', $array);

$tr='';
foreach($array as $i=>$ar){
    if($key == $i){
        $tr .= '<a style="color:red">'.$ar. '</a> '; 
    }else{
        $tr .= $ar.' ';      
    }
}
echo $tr;//this is a <a style="color:red">test</a> for <a style="color:red">testing</a> a <a style="color:red">test</a> function

search -> 'tes';
echo after find -> this is a "test" for "testing" a "test" function

A:

$term="testing a test";
function search_array($array, $term)
{       
    $keys=array();
    foreach ($array AS $key => $value) {
        $term1 = explode(' ',$term);
        foreach ($term1 AS $k => $t) {
            if (stristr($value, $t) === FALSE) {
                continue;
            } else {
                $keys[]=$key;
            }
        }
    }

    return $keys;
}
$str = "this is a test for testing a test function"; 
$array = explode(' ',$str);
$data=search_array($array, $term); //get keys
$tr='';
foreach($array as $i=>$ar){
    if(in_array($i,$data)){//if avialable
        $tr .= '<a style="color:red">'.$ar. '</a> '; 
    }else{
        $tr .= $ar.' ';      
    }
}
echo $tr;

