如何在php中检查数组中是否存在单词?

如何在php中检查数组中是否存在单词?,php,curl,Php,Curl,我使用curl进行网页抓取,可以显示感兴趣的结果 通常,下面的脚本会向我输出WEB SCRAPER测试场文本,该文本由页面中的“title”id进行刮取和正则化 现在我想检查$list数组中是否存在单词“TESTING”。如果是-只回显“存在”,如果不是-回显“不存在”。最好的方法是什么 我知道如何搜索网页并从中提取文本部分 $curl = curl_init('http://testing-ground.scraping.pro/textlist'); // cURL setup cu

我使用curl进行网页抓取,可以显示感兴趣的结果

通常,下面的脚本会向我输出WEB SCRAPER测试场文本,该文本由页面中的“title”id进行刮取和正则化

现在我想检查$list数组中是否存在单词“TESTING”。如果是-只回显“存在”,如果不是-回显“不存在”。最好的方法是什么

我知道如何搜索网页并从中提取文本部分

 $curl = curl_init('http://testing-ground.scraping.pro/textlist'); // cURL 
 setup

curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); //  return the transfer 
page as a string
curl_setopt($curl, CURLOPT_HEADER, TRUE);


$page = curl_exec($curl); // executing the request

if(curl_errno($curl)) // check for execution errors
{
    echo 'Scraper error: ' . curl_error($curl);
    exit;
}

curl_close($curl); // closing the connection

$regex = '/<div id="title">(.*?)<\/div>/s'; // extracting the needed part

if ( preg_match($regex, $page, $list) ) // search matches of $page with 
$regex
    echo $list[0];
else
    print "Not found";
$curl=curl\u init('http://testing-ground.scraping.pro/textlist'); // 卷曲
设置
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);//退回转帐单
页面作为字符串
curl_setopt($curl,CURLOPT_头,TRUE);
$page=curl_exec($curl);//执行请求
if(curl\u errno($curl))//检查执行错误
{
回显“刮片错误:”。卷曲错误($curl);
出口
}
curl_close($curl);//关闭连接
$regex='/(.*?)/s';//提取所需的部分
if(preg_match($regex,$page,$list))//使用搜索$page的匹配项
$regex
echo$list[0];
其他的
打印“未找到”;
我知道如何搜索网页并从中提取文本部分

 $curl = curl_init('http://testing-ground.scraping.pro/textlist'); // cURL 
 setup

curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); //  return the transfer 
page as a string
curl_setopt($curl, CURLOPT_HEADER, TRUE);


$page = curl_exec($curl); // executing the request

if(curl_errno($curl)) // check for execution errors
{
    echo 'Scraper error: ' . curl_error($curl);
    exit;
}

curl_close($curl); // closing the connection

$regex = '/<div id="title">(.*?)<\/div>/s'; // extracting the needed part

if ( preg_match($regex, $page, $list) ) // search matches of $page with 
$regex
    echo $list[0];
else
    print "Not found";
事实上,你正在这么做。 代码实际上应该看起来像

$list[]=(@DOMDocument::loadHTML($page))->getElementById("title")->textContent;
如果您想学习如何在PHP中正确解析HTML,请阅读该线程>

现在我想检查$list数组中是否存在单词“TESTING”。如果是-只回显“存在”,如果不是-回显“不存在”。最好的方法是什么

找到一个bool,用foreach迭代,用strpos()检查列表中的每个条目,如果找到它,确保尽早中断循环(因为找到匹配项后继续循环将浪费cpu和时间),最后打印结果,例如

$found=false;
foreach($list as $foo){
    if(false!==strpos("TESTING",$foo)){
        $found=true;
        break;
    }
}
if($found){
    echo "present";
}else{
    echo "not present";
}
我知道如何在数组()中搜索网页并从中提取文本部分?