我有正则表达式:
$reg = '/<a class="title".*>(.*)<\/a>/';
以及以下文字:
$text = '<h3 class="carousel-post-title"><a class="title" href="/first-link/">Some text<br /><span class="title-highlight">with a span</span></a></h3>'
我传递给preg_match:
$matches = [];
preg_match($reg, $text, $matches);
返回
Array (
[0] => <a class="title" href="/first-link/">Some text<br /><span class="title-highlight">with a span</span></a>
[1] =>
)
,而
$text2 = '<h3 class="carousel-post-title"><a class="title" href="/second-link/">Some text here</a></h3>';
preg_match($reg, $text2, $matches);
返回
Array
(
[0] => <a class="title" href="/second-link/">Some text here</a>
[1] => Some text here
)
为什么?为什么子模式&#34;(。*)&#34;不匹配&#39;有跨度&#39;?
答案 0 :(得分:1)
将您的模式更改为
$reg = '/<a class="title"[^>]*>([^<]*)<\/a>/';
除非它在第一部分是<
或在第二部分是>
,否则它知道你想要任何东西。
<a class="title"[^>]*> //Get the opening tag
([^<]*) //match anything until you reach a closing tag
<\/a> // your closing tag