Stark
Stark

Reputation: 593

How to only get text when fetching data between elements using regex?

I'm extracting and breaking up the data from the <strong></strong> tags, take the following:

<li><strong>2</strong> apples</li>
<li><strong>5</strong> rotten oranges</li>
<li><b>3</b> pears</li>
<li><strong>4 and</strong><strong>5</strong> not working</li>

Right now it's getting everything between the first and last <strong> tag, but I don't want to return the inner html tag inside the match.

The array being returned is echoing the following: 4 and</strong><strong>5 but I just want 4 and 5 in the array and same line.

This is the regex I've come up with: <(?:strong|b)>(.*)(?:<\/(?:strong|b)>)(.*)

http://www.phpliveregex.com/p/lgH

Upvotes: 0

Views: 44

Answers (1)

Harlan Wilton
Harlan Wilton

Reputation: 504

If you're fine looking through the results of each line recursively, putting a lazy quantifier fixes your issue. You also don't need the second capturing group

<(?:strong|b)>(.*?)(?:<\/(?:strong|b)>)

See here - https://regex101.com/r/pk7gVe/1/

Upvotes: 1

Related Questions