我正在尝试监控一个网站的新产品页面,其中包含特定的单词。我已经有了一个使用file_get_contents();
搜索单个单词的基本脚本,但是这是无效的。
查看代码它们位于<table>
<td>
标签中我如何让PHP搜索单词,不管他们是什么顺序和得到声明?例如
$searchTerm = "Orange Boots";
来自:<table>
<td>Boots (RED)</td>
</table>
<table>
<td>boots (ORANGE)</td>
</table>
<table>
<td>Shirt (GREEN)</td>
</table>
返回匹配项。
对不起,如果不是很清楚,但我希望你能理解
你可以这样做
$newcontent= (str_replace( 'Boots', '<span class="Red">Boots</span>',$cont));
然后为class red写入css就像你想要显示红色而不是color:red;
一样对rest
如果您希望对该HTML块进行快速搜索,可以尝试使用preg_match_all()函数使用一个简单的正则表达式。例如,您可以尝试:
$html_block = get_file_contents(...);
$matches_found = preg_match_all('/(orange|boots|shirt)/i', $html_block, $matches);
$matches_found
将是1或0,作为是否找到匹配的指示。$matches
将按照任何匹配项填充
使用curl。它比filegetcontents()快得多。这里是一个起点:
$target_url="http://www.w3schools.com/htmldom/dom_nodes.asp";
// make the cURL request to $target_url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html= curl_exec($ch);
if (!$html) {exit;}
$dom = new DOMDocument();
@$dom->loadHTML($html);
$query = "(/html/body//tr)"; //this is where the search takes place
$xpath = new DOMXPath($dom);
$result = $xpath->query($query);
for ($i = 0; $i <$result->length; $i++) {
$node = $result->item(0);
echo "{$node->nodeName} - {$node->nodeValue}<br />";
}