PHP 正则表达式 return 零结果

PHP regex return zero result

我有以下 PHP curl 和正则表达式代码。我想从网站上获取 post header。实际上,有 10 篇文章。但代码 returns 结果为零。

PHP:

<?php 
$ch = curl_init();
$url = "www.mahsumakbas.net";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$content = curl_exec($ch);
curl_close($ch);

@preg_match_all('/<h2 class="entry-title">(.*)<\/h2>/' ,$content, $matches); 


for ($i=0;  $i< sizeof($matches[1]); $i++)
    echo $matches[1][$i]."<br/>";

?>

在 www.mahsumakbas.net 网页上有 10 个 <h2 class="entry-title"> 包含在 </h2>

我想念什么?

试试这个:

$url = "www.mahsumakbas.net";
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
//curl_setopt(... other options you want...)
$html = curl_exec($c);

curl_close($c);
preg_match_all("'<h2 class=\"entry-title\">(.*?)</h2>'si" ,$html, $matches); 

foreach($matches[1] as $key=>$val)
    echo $val."<br/>";

您的标题分为 3 行。您必须设置 "m" 选项。也许有帮助。

http://php.net/manual/en/reference.pcre.pattern.modifiers.php

但要解析 HTML-DOM 字符串,您应该使用 DOMDocument 和 getElementByTagName