strpos存储所有结果不是第一个积极的

I have this code.

$needles = explode(" ", $textContentArray); 
$haystack = $textContent; 
$match_found = strposa($haystack,$needles);
if($match_found) {
    echo $match_found ; 
}
else {
    echo "No match found.";
}

function strposa($haystack, $needles) { 
$chr = array();
foreach($needles as $needle) {
    $res = stripos($haystack, $needle);
    if ($res !== false)
        {
            $chr[$needle] = $res;
            $string_exist = $needle; break; 
        }
    }
    if(empty($chr)) return false; 
    return $string_exist;
}

Currently this outputs the first match it finds, however, I'd like it to output every match not just the first. This is because I'm using it on a site which will show any string matches between 2 url's.

Thank you.

just return matches in your function

    if(empty($chr)) return false; 
    return $chr;
}

and remove break

so $match_found will be array for example ["foo" => 1, "bar" => 4, ...]

$textContentArray = preg_replace('/(\{|\}|\[|\]|\(|\)|\||\^|\$)/', '\\\$1', $textContentArray);
$needles = array_map('trim',array_filter(explode(' ',$textContentArray)));
preg_match_all('#('.implode('|',$needles).')#is', $haystack, $result);

if(isset($result[1])){
   echo '<pre>';
   print_r($result);
   echo '</pre>';
}else{
   echo 'not found';
};