PHP脚本,用于下载网页的源代码并搜索特定字符串

I need help crafting a PHP code that accomplishes the following:

  1. Access a website (www.example.com)
  2. Download its source code into a string variable
  3. Search this specific string for a specific content such as

    <div class="news" title="news alert">Click to get news alert</div>

Basically I need to search the source code for title="news alert"

Thank you all,

That's pretty simple:

$html = file_get_contents('http://site.com/page.html');
if (strpos($html,'title="news alert"')!==false)
 echo 'title found';

You could use PHP DOM:

$text = file_get_contents('http://example.com/path/to/file.html');
$doc = new DOMDocument('1.0');
$doc->loadHTML($text);
foreach($doc->getElementsByTagName('div') AS $div) {
    $class = $div->getAttribute('class');
    if(strpos($class, 'news') !== FALSE) {
        if($div->getAttribute('title') == 'news alert') {
            echo 'title found';
        }
        else {
            echo 'title not found';
        }
    }
}

Or perhaps Query Path which is tries to emulate jQuery server side:

$text = file_get_contents('http://example.com/path/to/file.html');
if(qp($text)->find('div.news[title="news alert"]')->is('*')) {
    echo('title found');
}
else {
    echo('title found');
}
$page = file_get_contents('http://www.example.com/');
if(strpos($page, "title=\"news alert\"")!==false){
    echo 'title found';
}
$url = 'http://www.example.com/';
$page = file_get_contents($url);

if(strpos($page, 'title="news alert"') !==false || strpos($page, 'title=\'news alert\'') !==false)
{
    echo 'website with news alert found';
}
else
{
    echo 'website not found';
}

You could use DOMXPath to find it:

$dcmnt = new DOMDocument(); $dcmnt->loadHTML( $cntnt );
$xpath = new DOMXPath( $dcmnt );
$match = $xpath->query("//div[@title='news alert']");

echo $match->length ? "Found" : "Not Found" ;

Demo: http://codepad.org/CLdE8XCQ