I have a problem with the code below which is driving me crazy. What I want to do is to compare a given array with a sentence and then I need to know their position in the sentence for each occurrence, by now the script only return just one array, for example with the positions in which the name Marta is found inside the sentence. I trying to merge all the results in just one array but I'm a bit lost at the moment. I hope someone can give me some clues to make it. Best regards.
$sentence = 'Maria is Maria and Marta is Marta.';
$womennames = array("Maria","Marta");
function poswomen($chain, $words){
foreach($words as $findme){
$valida_existe = substr_count($chain,$findme);
$largo_encuentra = strlen($findme);
$posicion = array();
for($x=0; $x < strlen($chain); $x++){
$posic_x = strpos($chain, $findme, $x);
if($posic_x >= 0){
$posicion[] = $posic_x;
$x = $x+$largo_encuentra;
}
}
$posicion = array_unique($posicion);
$posicion = implode(",",$posicion);
}
return $posicion;
}
poswomen($sentence, $womennames);
print_r (poswomen($sentence, $womennames));
$posicioneach at the beginning of each iteration of theforeacharray. You should initialize it outside the loop.