Preg Match for multiple attributes.

Posted on 2004-11-29
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
    LVL 48

    Accepted Solution

    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

    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

    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

    Thanks for your help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now