Rhyono
Rhyono

Reputation: 2468

Match word in PHP regex to not include

I want to fetch <FONT COLOR="#0000ff">(insert some text here)<\/FONT> with preg_match. Lines similar to that are repeating and touching each other, so if I try something generic like <FONT COLOR="#0000ff">(?<NAME>+)<\/FONT> it'll fetch all of them as one cell. So I threw on [^<] to make it stop at the first less than it encountered, thus keeping it from including HTML and therefore being one cell...

However, the text I'm fetching sometimes contains HTML that I want. Is there a way to use [^] with </F? My attempts at a negative look ahead did not work because it goes "yep, there's definitely a closing font tag on the end. Better not return anything."

Upvotes: 0

Views: 120

Answers (1)

Shubham
Shubham

Reputation: 22317

If I understand you correctly, you need a RegEx to perform non-greedy match. Eg,

If query string is: <x> abc </x> <x> def </x> <x> hig </x>

Our RegEx is: /<x>(.*)<\/x>/

Then a greedy match is performed and what is returned is the whole query string because it satisfies the RegEx. To avoid it we use, non-greedy matching.

New RegEx: /<x>(.*?)<\/x>/

It should return all three tags with preg_match_all()

Upvotes: 2

Related Questions