Why can't I catch this tag???

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Xonox
Forum Newbie
Posts: 2
Joined: Tue Apr 07, 2009 8:42 pm

Why can't I catch this tag???

Post by Xonox »

Hi there,

I have a problem with catching tags, hope you can help. The regex works in all situations except the one I'm presenting, I'm trying to catch tags in the form

Code: Select all

<tag />
, without nesting.

My regex:

Code: Select all

preg_match_all('/(<(' . $ftag . '+)[^>](.*)(\/>))/', $string, $matches, PREG_SET_ORDER);
It worked fine catching all the single tags except on this situation:

Code: Select all

$string = '<img src="<field name="media_file" />" width="<field name="media_width" />" height="<field name="media_height" />" border="0" alt=" <field name="media_description" /> " />'
Has you might figure I want to get

Code: Select all

<field name="media_file" />
<field name="media_width" />
<field name="media_height" />
<field name="media_description" />
But it returns one single wrong tag, like this:

Code: Select all

<snapfield name="media_file" />" width="<snapfield name="media_width" />" height="<snapfield name="media_height" />" border="0" alt=" <snapfield name="media_description" /> " />
I can't figure out what's the problem. Any help would be appreciated, thanks.
Xonox
Forum Newbie
Posts: 2
Joined: Tue Apr 07, 2009 8:42 pm

Re: Why can't I catch this tag???

Post by Xonox »

I just figured it out...

I just made the regex ungreedy adding a "?"

It's allways a detail that drives you crazy. :P

Code: Select all

preg_match_all('/(<(' . $ftag . '+)[^>](.*?)(\/>))/', $string, $matches, PREG_SET_ORDER);
Thanks anyway, i hope this helps someone else! Cheers!
Post Reply