按数组字符串显示数据结果匹配 PHP
Show Data Result Match by Array String PHP
两天前使用 PHP 简单的 DOM 解析器学习抓取,但是在通过给出的特定单词查找结果时遇到问题“strpos(): needle is not a string or an integer”像下面的代码一样排列。我错过了什么?
$this->load->library('simple_html_dom');
$html = file_get_html('https://id.priceprice.com/harga-hp/');
$konten = $html->find('h3');
$keyword = array('Samsung', 'Huawei', 'Iphone');
foreach ($konten as $e) {
if (strpos($e->plaintext, $keyword) !== false) {
echo $e->plaintext . "</br>";
}
}
错误消息,根据 OP 的评论:
strpos(): needle is not a string or an integer
$keyword是一个数组,需要foreach()
循环遍历,例如:
foreach ($konten as $e) {
foreach ($keyword as $kw) {
if (strpos($e->plaintext, $kw) !== false) {
echo $e->plaintext . "</br>";
}
}
}
参见php函数strpos():
strpos ( string $haystack , string $needle , int $offset = 0 ) : int|false
两天前使用 PHP 简单的 DOM 解析器学习抓取,但是在通过给出的特定单词查找结果时遇到问题“strpos(): needle is not a string or an integer”像下面的代码一样排列。我错过了什么?
$this->load->library('simple_html_dom');
$html = file_get_html('https://id.priceprice.com/harga-hp/');
$konten = $html->find('h3');
$keyword = array('Samsung', 'Huawei', 'Iphone');
foreach ($konten as $e) {
if (strpos($e->plaintext, $keyword) !== false) {
echo $e->plaintext . "</br>";
}
}
错误消息,根据 OP 的评论:
strpos(): needle is not a string or an integer
$keyword是一个数组,需要foreach()
循环遍历,例如:
foreach ($konten as $e) {
foreach ($keyword as $kw) {
if (strpos($e->plaintext, $kw) !== false) {
echo $e->plaintext . "</br>";
}
}
}
参见php函数strpos():
strpos ( string $haystack , string $needle , int $offset = 0 ) : int|false