Preg Match for multiple attributes.

Posted on 2004-11-29
Medium Priority
Last Modified: 2013-12-12
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.

Question by:Ape
  • 2
  • 2
LVL 48

Accepted Solution

hernst42 earned 2000 total points
ID: 12695345
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);

Author Comment

ID: 12704625
Thanks.  Is there reallly no other way of doing it?  Or at least checking for order of attributes within regualr expressions at all?
LVL 48

Expert Comment

ID: 12708266
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.

Author Comment

ID: 12714718
Thanks for your help.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question