-2

I'm trying to use a regex to find a string. For example, my text is :

$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>"

I want to find :

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

I try this pattern :

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

if i echo $matches[1] it will return:

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

and if i echo $matches[2] it will return:

" of AAA<p>"

how to get $matches that match to this sentence :

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

Actually i want to make a pattern that match a text inside a tag not a

tag.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Arifin _
  • 5
  • 3
  • 1
    Don't just post pictures. Write your question in the post. – Geshode Jul 20 '18 at 03:06
  • Please read [ask], then [edit] your question and provide a [mcve] (this means, your _code must be in the question itself, **and your actual question must be in the question itself**_). – Sebastian Simon Jul 20 '18 at 03:07
  • how to post question with a tag. it change my text. My

    tag will return a bold text

    – Arifin _ Jul 20 '18 at 03:08

1 Answers1

0

Instead of using a regex to parse your html, you might use DOMDocument and 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();
    }
}

That will result in:

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

Demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70