gpt4 book ai didi

PHP 在远程网页中搜索字符串

转载 作者:行者123 更新时间:2023-12-05 05:26:21 25 4
gpt4 key购买 nike

我正在尝试用 PHP 编写一个脚本,它将在其中打开一个文本文件 ./urls.txt 并检查每个域中的特定单词。我真的是 PHP 新手。

例子:在以下域中查找“Hello”一词。列表:Domain1.LTDDomain2.LTDDomain3.LTD

然后简单地打印出域名+有效/无效。

<?PHP
$link = "http://yahoo.com"; //not sure how to loop to read each line from a file.
$linkcontents = file_get_contents($link);

$needle = "Hello";
if (strpos($linkcontents, $needle) == false) {
echo "Valid";
} else {
echo "Invalid";
}
?>

最佳答案

$arrayOfLinks = array(
"http://example.com/file.txt",
"https://www.example-site-2.com/files/file.txt"
);

$needle = "Hello";

foreach($arrayOfLinks as $link){ // loop through the array

$linkcontents = file_get_contents($link);

if (stripos($linkcontents , $needle) !== false) { // stripos is case-insensitive search
// the needle exists in $linkcontents
// !== false instead of != false since stripos can return 0 meaning the needle is the first word of the contents
echo "Valid";
} else {
// the word does not exist in the given text
echo "Invalid";
}
}

关于PHP 在远程网页中搜索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26743350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com