Preg Match for multiple attributes.

I’ve a problem writing a few regular expressions that will all take more or less the same format.  The issue is with recognising the order that words/matches may appear.  Examples of what I am trying to match:

[IMG: XX caption=my caption align=left]

This would be replaced with <img src="myfile.jpg" alt="my caption" align="left"  />

Where myfile.jpg is the image id referenced by XX.

Given that the caption and align attributes might appear in any order I’d like to be able to match on the above or the variation below:

[IMG: XX align=right caption=my caption 2]


In addition, the caption and align attributes are optional and mightn’t necessarily be there at all.

Similarly I am wanting to match the src and align attributes on a standard image html tag:

<img src=”myfile.jpg” align=”left” />

Again the issue here is that the attributes could be anywhere within the image tag itself.

What I have so far:


                        $pattern = '/\[IMG: ([0-9])+ (caption=(.*?)) align=(left|right)]/is'; #match image tags

                        $html_body = preg_replace_callback($pattern, array($this, '_fetchImage'), $html_body);  

This however will only match if caption is the first attribute.

Any solution to this would be very much appreciated.

Thanks.
ApeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hernst42Commented:
If can have such attributes in different orders there is no way to do it in one regular-expression. Your approach with the callback is a good one.
You capture the whole tag via regular expression and pass that part to the function. Inside the function you search for the different attributes and build the result you need inside the function. So the callback-function has more knowledge.

$pattern = '/\[IMG:([^\]]+)\]/'/is'; #match image tags
preg_replace_callback($pattern, array($this, '_fetchImage'), $html_body);  

function _fetchImage($m) {
    preg_match('/caption=([^\s]+)/', $m[1], $c);
...
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ApeAuthor Commented:
Thanks.  Is there reallly no other way of doing it?  Or at least checking for order of attributes within regualr expressions at all?
0
hernst42Commented:
with regex it might be possible, but you will need to write all possible occurences into the regex. Even to check sorting must be done the same way. For such a case a parser is needed, one regex will be too complicated, so do it in two passes.
0
ApeAuthor Commented:
Thanks for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.