给定这个字符串:
$myString = '<details class="myEl" open="open">
<summary>In this article</summary>
<ol>
<li><a href="post-slug/">Introduction</a></li>
<li><a href="post-slug/2/">Title for the second page</a></li>
<li><a href="post-slug/3/">Title for the third page</a></li>
</ol>
</details>';
如果我给定要搜索的匹配是"/2/",那么将使用什么正则表达式来拉出"第二页的标题"?
我还需要拉"Title for the third page",如果匹配是"/3/",所以我需要一个通用的正则表达式,将拉字符串之间的>和<</p>
试试这个:
preg_match('!'/' . $pageNo . ''/">(.*?)'<'/a'>!', $myString, $matches);
$pageTitle = $matches[1];
编辑:第1页应该使用这个:
preg_match('!'/' . ($pageNo == 1 ? ($pageNo . ''/') : '') . '">(.*?)'<'/a'>!', $myString, $matches);
$pageTitle = $matches[1];
我想最好使用XPath来做这样的事情,一个例子是:
$str = '<details class="myEl" open="open">
<summary>In this article</summary>
<ol>
<li><a href="post-slug/">Introduction</a></li>
<li><a href="post-slug/2/">Title for the second page</a></li>
<li><a href="post-slug/3/">Title for the third page</a></li>
</ol>
</details>';
$xml = simplexml_load_string($str);
var_dump($xml->xpath('//details/ol/li/a[contains(@href, "/3/")]'));
但是对于正则表达式,下面的正则表达式可以完成工作:
preg_match_all('@<li><a href="post-slug/3/">((?:(?!<'/a>).)+)</a></li>@', $str, $matches);
print_r($matches);