带有正则表达式的标签内的Preg_match模式

时间:2018-07-20 03:04:28

标签: php regex

我正在尝试使用正则表达式来查找字符串。例如,我的文字是:

$text="<h2>Introduction of Abdominal aortic aneurysm</h2>
<p>In this section, we will learn about symptom, causes, and treatment of 
AAA</p>
<h2>This is Treatment for a burst AAA</h2><p>.......<p>
<h2>.........</h2>"

我想找到:

$temp="<h2>This is Treatment for a burst AAA</h2>
<p>.......<p>"

我尝试这种模式:

Preg_match("/<h2(.*?)Treatment(.*?)<h2>/i",$text,$matches);

如果我回显$ matches [1],它将返回:

"Introduction of Abdominal aortic aneurysm</h2>
<p> In this section, we will learn about symptom, causes, and "

如果我回显$ matches [2],它将返回:

" of AAA<p>"

如何获得与此句子匹配的$ matches:

"<h2>This is Treatment for a burst AAA</h2>"

实际上,我想制作一种与标签而非

标签内的文本匹配的图案。

1 个答案:

答案 0 :(得分:0)

您可以使用regex to parse your htmlDOMDocument来代替C14N

$dom = new DOMDocument();
$text="<h2>Introduction of Abdominal aortic aneurysm</h2>
<p>In this section, we will learn about symptom, causes, and treatment of 
AAA</p>
<h2>This is Treatment for a burst AAA</h2><p>.......<p>
<h2>.........</h2>";

$dom->loadHTML($text);
$elms = $dom->getElementsByTagName("h2");
foreach($elms as $elm) {
    if (strstr($elm->nodeValue, "Treatment") !== false) {
        echo $elm->C14N();
    }
}

这将导致:

<h2>This is Treatment for a burst AAA</h2>

Demo