Ii want to find specific words in a text. Therefore I build an array and turn the values over to the strpo_arr
-function. Here I split a textarea
into its sentences and look in every sentence about these specific words, to highlight this sentence. This works, but if I have a target on the first sentence of this sentences-Array, the output of the part $sentence
is empty. Is there anything I'm doing wrong? Many thanks!!
function strpos_arr($haystack, $needle) {
if(!is_array($needle)) $needle = array($needle);
foreach($needle as $what) {
if(($pos = strpos($haystack, $what))!==false) return $pos;
}
return false;
}
$sentence = " ";
foreach ($sentences as $sentence) {
if(strpos_arr($sentence, $searchStringArr)){
$subContent .= "<span class='highlight'> $sentence</span> ";
}else{
$subContent .= " ".$sentence;
}
}