preg_match 不匹配带有 html 标签的子模式
preg_match not matching subpattern with html tag
我有一个正则表达式:
$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);
这个returns
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);
returns
Array
(
[0] => <a class="title" href="/second-link/">Some text here</a>
[1] => Some text here
)
这是为什么?为什么子模式“(.*)”与 'with a span' 不匹配?
将模式更改为
$reg = '/<a class="title"[^>]*>([^<]*)<\/a>/';
这样它就知道您想要任何东西,除非它是第一部分的 <
或第二部分的 >
。
<a class="title"[^>]*> //Get the opening tag
([^<]*) //match anything until you reach a closing tag
<\/a> // your closing tag
我有一个正则表达式:
$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);
这个returns
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);
returns
Array
(
[0] => <a class="title" href="/second-link/">Some text here</a>
[1] => Some text here
)
这是为什么?为什么子模式“(.*)”与 'with a span' 不匹配?
将模式更改为
$reg = '/<a class="title"[^>]*>([^<]*)<\/a>/';
这样它就知道您想要任何东西,除非它是第一部分的 <
或第二部分的 >
。
<a class="title"[^>]*> //Get the opening tag
([^<]*) //match anything until you reach a closing tag
<\/a> // your closing tag