我需要帮助制作能够完成以下任务的PHP代码:
在此特定字符串中搜索特定内容,例如
<div class="news" title="news alert">Click to get news alert</div>
基本上我需要搜索title="news alert"
谢谢大家,
答案 0 :(得分:3)
您可以使用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';
}
}
}
或者Query Path试图模仿jQuery服务器端:
$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');
}
答案 1 :(得分:1)
您可以使用DOMXPath找到它:
$dcmnt = new DOMDocument(); $dcmnt->loadHTML( $cntnt );
$xpath = new DOMXPath( $dcmnt );
$match = $xpath->query("//div[@title='news alert']");
echo $match->length ? "Found" : "Not Found" ;
答案 2 :(得分:0)
这很简单:
$html = file_get_contents('http://site.com/page.html');
if (strpos($html,'title="news alert"')!==false)
echo 'title found';
答案 3 :(得分:0)
$page = file_get_contents('http://www.example.com/');
if(strpos($page, "title=\"news alert\"")!==false){
echo 'title found';
}
答案 4 :(得分:0)
$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';
}