簡體   English   中英

如何回顯與給定字符串匹配的數組中的值

[英]How do I echo the values in the array that matched in the given string

我需要 PHP 代碼方面的幫助。

我想回顯數組$finds中與給定字符串匹配的字符串。

例如,如果我輸入“https://amazon.com/”,結果將是:

!Found - [ https://amazon.com/ ] - [ 2 keywords was found in the site. [Which word / words from $finds was found.] ]

這是我的實際代碼:

$ii = [
'cookie' => mt_rand().'.txt'

];

if (!is_dir($pathhhhhh)) {  
    mkdir($pathhhhhh, 0777, true);
}
$d1 = getcwd();
$d = str_replace('\\', '/', $d1);

$g = curl_init();
curl_setopt($g, CURLOPT_COOKIEJAR, "".$d."/COOKIE/".$ii['cookie']."");
curl_setopt($g, CURLOPT_COOKIEFILE, "".$d."/COOKIE/".$ii['cookie']."");
curl_setopt($g, CURLOPT_URL, ''.$hehe.'');
curl_setopt($g, CURLOPT_HTTPGET, 1);
curl_setopt($g, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($g, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($g, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($g, CURLOPT_SSL_VERIFYHOST, 0);
$g1 = curl_exec($g);
$string = html_entity_decode(htmlentities($g1));
$finds = ['shop'];
$findCount = 0;


foreach ($finds as $find) {
    if ( stripos($string, $find) !== false ) $findCount++;
}

if ($findCount != 0){
    if($findCount > 1){
        $ident = 'Keywords';
    }else{
        $ident = 'Keyword';
    }
    $res = "!Found - [$hehe] - [ $findCount $ident was found in the site. ]";
    echo "<font size='4' color='#39ff14'><tt><i>$res</tt></i></font><br>";

} 
elseif (empty($string)) {
    $res = "!Error - [$hehe] - [ An error occurred during the scraping. ]";
    echo "<font size='4' color='#ff073a'><tt><i>$res</tt></i></font><br>";
} 
else {
    $res = "!Error - [$hehe] - [ No Match Found ]";
    echo "<font size='4' color='#ff073a'><tt><i>$res</tt></i></font><br>"; 
}

您可以將$find添加到數組中,並使用implode()到 output 這些詞。

$hehe = 'example.com';
$string = "A sample sentence with words like shop.";

$finds = ['shop'];
$founds = [];

foreach ($finds as $find) {
    if ( stripos($string, $find) !== false ) {
        $founds[] = $find;
    }
}



if (!empty($founds)) {
    $findCount = count($founds);
    if ($findCount > 1){
        $ident = 'Keywords';
    }
    else {
        $ident = 'Keyword';
    }
    $words = implode(', ', $founds);
    $res = "!Found - [$hehe] - [ $findCount $ident was found in the site : $words]";
    echo "<font size='4' color='#39ff14'><tt><i>$res</tt></i></font><br>";
} 

這將 output:

!Found - [example.com] - [ 1 Keyword was found in the site. shop]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM