Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将URL放入数组并在数组中搜索匹配字符串?_Php_Arrays_String - Fatal编程技术网

Php 如何将URL放入数组并在数组中搜索匹配字符串?

Php 如何将URL放入数组并在数组中搜索匹配字符串?,php,arrays,string,Php,Arrays,String,我试图在URL中搜索匹配的字符串,但下面的代码片段似乎不起作用 <?php $url = "http://www.drudgereport.com"; $search = "a"; $file = file($url); if (in_array($search,$file)) { echo "Success!"; } else { echo "Can't find word."; } ?> _array()中的检查数组成员是否等于您的指针 许多网站不可能只

我试图在URL中搜索匹配的字符串,但下面的代码片段似乎不起作用

<?php

$url = "http://www.drudgereport.com";

$search = "a";
$file = file($url);

if (in_array($search,$file)) {
    echo "Success!";
} else {
    echo "Can't find word.";
}

?>
_array()中的
检查数组成员是否等于您的指针

许多网站不可能只有一条线等于
a

此外,是否已启用?

in_array()
检查数组成员是否等于您的指针

许多网站不可能只有一条线等于
a


此外,是否已启用?

该代码将只查找具有确切的
$search
字符串(可能包括空格)的行。如果您正在解析HTML,请检查PHP的类。或者,您可以使用正则表达式来提取所需的内容。

该代码将只找到包含确切的
$search
字符串(可能包括空格)的行。如果您正在解析HTML,请检查PHP的类。或者,您可以使用正则表达式来提取所需内容。

如果您只是在页面上搜索字符串的出现,您可以使用

$str = file_get_contents($url);
if (strpos($str, $search) !== false) {
    echo 'Success!';
} else {
    echo 'Fail';
}

如果您只是在页面上搜索字符串,可以使用

$str = file_get_contents($url);
if (strpos($str, $search) !== false) {
    echo 'Success!';
} else {
    echo 'Fail';
}

正如@alex所说,检查是否允许\u url\u fopen已启用。
您还可以使用搜索字符串:

<?php

$url = "http://www.drudgereport.com";

$search = "a";
$file_content = file_get_contents($url);

if (strpos($file_content, $search) !== false) {
    echo "Success!";
} else {
    echo "Can't find word.";
}

?>

正如@alex所说,检查是否允许\u url\u fopen已启用。
您还可以使用搜索字符串:

<?php

$url = "http://www.drudgereport.com";

$search = "a";
$file_content = file_get_contents($url);

if (strpos($file_content, $search) !== false) {
    echo "Success!";
} else {
    echo "Can't find word.";
}

?>

例如,我如何循环遍历$str的全部内容并计算a的数量?@Matt,您不需要循环,因为整个页面都作为字符串加载到
$str
中。要计算另一个字符串中某个字符串的出现次数,请尝试
substr\u count()
()。例如,如何循环遍历$str的全部内容并计算a的数量?@Matt,您不需要循环,因为整个页面都是作为字符串加载到
$str
中的。要计算另一个字符串中某个字符串的出现次数,请尝试
substr\u count()
()。