php脚本从文件中搜索多个网页以查找特定单词

php脚本从文件中搜索多个网页以查找特定单词,php,Php,首先请原谅我英语不好 我试图构建一个php脚本,从一个.txt文件中搜索多个网页以查找特定的单词 更具体地说: 我有一个.txt文件,其中存储了许多url(每个url都在一行上,因此,如果我有10个url,则该文件有10行),我希望脚本检查每个url的网页内容以查找特定单词。因此,如果在网页上找到该词,脚本将返回联机,否则将返回向下 我构建了这个脚本,但问题是它总是在线返回,即使来自文件的url在其网页内容中没有特定的单词 <?php $allads = file("phpelist.

首先请原谅我英语不好

我试图构建一个php脚本,从一个.txt文件中搜索多个网页以查找特定的单词

更具体地说:

我有一个.txt文件,其中存储了许多url(每个url都在一行上,因此,如果我有10个url,则该文件有10行),我希望脚本检查每个url的网页内容以查找特定单词。因此,如果在网页上找到该词,脚本将返回联机,否则将返回向下

我构建了这个脚本,但问题是它总是在线返回,即使来自文件的url在其网页内容中没有特定的单词

<?php  
$allads = file("phpelist.txt");  
print("Checking urls: <br><br><br><strong>");  
for($index = 0; $index <count($allads); $index++)  
{  
$allads[$index] = ereg_replace("\n", "", $allads[$index]);  
$data = file_get_contents('$allads[$index]');  
$regex = '/save/';  
if (preg_match($regex, $data)) {  
echo "$allads[$index]</strong>...ONLINE<br><strong>";  
} else {  
echo "$allads[$index]</strong>...DOWN<br><strong>";  
}  
}  
print("</strong><br><br><br>I verified all urls from file!");  
?

要在特定网页中搜索给定字符串,我会使用(不区分大小写)或(区分大小写)代替正则表达式:

if( stripos(haystack, needle) !== FALSE ) {
   //the webpage contains the word
}
例如:

$str = 'sky is blue';
$wordToSearchFor = 'sky';

if (strpos($str, $wordToSearchFor) !== false) {
    echo 'true';
}
else {
    echo 'Uh oh.';
}

尽管如此,通过程序浏览网页被认为不是一种好的做法,除非绝对必要,否则不应该这样做

更新:

在您的
文件中\u获取\u内容
呼叫您正在执行的操作:

$data = file_get_contents('$allads[$index]');  
您使用的是单引号,变量值不会被替换。您必须使用双引号让
file\u get\u contents
获取实际URL。替换为:

$data = file_get_contents("$allads[$index]");  
我注意到的另一件事是,您在代码中使用了弃用的
ereg\u replace()
函数。非常不鼓励依赖不受欢迎的函数

经过上述所有更正后,您的代码应该如下所示:

$allads = file("phpelist.txt");  
print("Checking urls: <br><br><br><strong>");  

for($index = 0; $index <count($allads); $index++)  
{  
    $allads[$index] = str_replace("\n", "", $allads[$index]);  
    $data = file_get_contents("$allads[$index]");  

    $searchTerm = 'the';  

    if (stripos($data, $searchTerm) !== false) {
        echo "$allads[$index]</strong>...ONLINE<br><strong>";  
    } 
    else 
    {  
        echo "$allads[$index]</strong>...DOWN<br><strong>";  
    }  
}  

print("</strong><br><br><br>I verified all urls from file!");  
?>
$allads=file(“phpelist.txt”);
打印(“检查URL:


”; 对于($index=0;$index
好的,如果您有时间,请给我举个例子,然后我将自定义我自己的脚本,谢谢。好的,在您的例子中,$str是URL(网页)?我做了更改,但对我文件中的所有URL仍然返回TRUE,即使其中一些URL不包含特定的单词。@toatatreaba:你能给我举一个这样的URL和单词的例子吗?@toatatreaba:确实有效。请看这个。