我正在复制我的strpos关键字检查,但我不想

I am pulling multiple values ($arr) from list of keywords ($keywords). Later I'm using strpos to check if that keyword existis in a document.

I could just duplicate the whole code and add every $arr manually one by one, like: strpos($element,$arr[0]); strpos($element,$arr[1]); strpos($element,$arr[2]);.

Can anyone help me set it up so that I don't have to do it the long way. Lets say I need a total of 20 $arr checks. That would be $arr[0] to $arr[19].

<?php

$str= "$keywords";
$arr=explode(",",$str);

// print_r($arr);

$data = file($_SERVER['DOCUMENT_ROOT'].'/posts.txt');

$data=str_replace(array('<', '>', '\\', '/', '='), "", $data);

foreach($data as $element) 

{

$element = trim($element);

$check_keyword = strpos($element,$arr[0]);
if ($check_keyword !== false) {
$pieces = explode("|", $element);   

echo "

" . $pieces[0] . "  

<a href=/blog/" . $pieces[2] . "/" . $pieces[3] . "/ ><b><font color=#21b5ec>" . $pieces[4] . "</font></b></a>  <br>

";    

}

else {
echo "";
}

}

?>

If you want to check every value in $arr against $element then the easiest way to do it is with array_reduce.

If you want to test whether every value in $arr is in $element, you can use this:

$check_keyword = array_reduce($arr, function ($carry, $item) use($element) {
        return $carry && (strpos($element, $item) !== false);
    }, true);

If you only want to check if any value in $arr is in $element, you can use this:

$check_keyword = array_reduce($arr, function ($carry, $item) use($element) {
        return $carry || (strpos($element, $item) !== false);
    }, false);