I need help with my PHP code.
I want to echo which string from the array $finds matched in the given string.
For example, if I input “https://amazon.com/” the result will be:
!Found - [ https://amazon.com/ ] - [ 2 keywords was found in the site. [Which word / words from $finds was found.] ]
Here is my actual code :
$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>";
}
Advertisement
Answer
You could add the $find in an array, and use implode() to output these words.
$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>";
}
This will output :
!Found - [example.com] - [ 1 Keyword was found in the site. shop]