php PHP获取URL内容并搜索字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/276602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Get URL Contents And Search For String
提问by zuk1
In php I need to get the contents of a url (source) search for a string "maybe baby love you" and if it does not contain this then do x.
在 php 中,我需要获取 url (source) 搜索字符串“maybe baby love you”的内容,如果它不包含此内容,则执行 x。
回答by okoman
Just read the contents of the page as you would read a file. PHP does the connection stuff for you. Then just look for the string via regex or simple string comparison.
只需像阅读文件一样阅读页面内容即可。PHP 为您完成连接工作。然后只需通过正则表达式或简单的字符串比较查找字符串。
$url = 'http://my.url.com/';
$data = file_get_contents( $url );
if ( strpos( 'maybe baby love you', $data ) === false )
{
// do something
}
回答by Tarek Ahmed
//The Answer No 3 Is good But a small Mistake in the function strpos() I have correction the code bellow.
$url = 'http://my.url.com/';
$data = file_get_contents( $url );
if ( strpos($data,'maybe baby love you' ) === false )
{
// do something
}
回答by Alan Storm
Assuming fopen URL Wrappers are on ...
假设 fopen URL Wrappers 处于...
$string = file_get_contents('http://example.com/file.html');
if(strpos ('maybe baby love you', $string) === false){
//do X
}
回答by Hugh Bothwell
If fopen URL wrappers are not enabled, you may be able to use the curl module (see http://www.php.net/curl)
如果未启用 fopen URL 包装器,您可以使用 curl 模块(请参阅http://www.php.net/curl)
Curl also gives you the ability to deal with authenticated pages, redirects, etc.
Curl 还使您能够处理经过身份验证的页面、重定向等。

