Regular expression problem

Hi,

I was wondering if anybody could help me out with small regex problem:
I need to define what alignment was selected (image) and replace and then change piece of coding accordingly.

I tried to use this piece:

/\\<img(.*?)(align="left")\\>/

but of course it does not work.

Can anybody help?
Thanks in advance!

Greg

Anthony, please correct me if I’m wrong, but if the HTML is not well-formed XML (e.g. <img/> is not self-closing) won’t DOMDocument fail completely? It depends on the OP’s confidence in the HTML’s quality as well I suppose, but I’m guessing it’s going to include code he’s not responsible for, which may well be malformed.

Can you describe what you want to achieve more accurately? Maybe give an example?

wave hand like a Jedi

RegExp is not the solution you’re looking for.


$html = '
<html>
    <head>
        <title>Page Title</title>
    </head>
    <body>
        <div id="container">
            <img align="left" src="/path/to/image.jpg" />
            <img src="/path/to/image.jpg" />
            <img align="right" src="/path/to/image.jpg" />
            <img align="" src="/path/to/image.jpg" />
        </div>
    </body>
</html>
';

$doc = new DOMDocument();
$doc->loadHtml($html);
foreach($doc->getElementsByTagName('img') as $image){
    if(true === $image->hasAttribute('align')){
        $image->setAttribute('align', 'right');
    }
}
echo $doc->saveHTML();

/*
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    <html>
        <head>
            <title>Page Title</title>
        </head>
        <body>
            <div id="container">
                <img align="right" src="/path/to/image.jpg">
                <img src="/path/to/image.jpg">
                <img align="right" src="/path/to/image.jpg">
                <img align="right" src="/path/to/image.jpg">
            </div>
        </body>
    </html>
*/

Indeed it will Raffles, I should have made this clear.

Thanks for the check. :cool:

Hey Fellas,

thank you very much for your replies!

I simply want to add a piece of html to the <img> tag. I have this wysiwyg that adds images, which works fine but the text around 'em is way too close.

So, depending on the alignment, I wanted to add padding to every image.

eg.
if align = left, then swap the second instance and add some css to the image
/<img(.*?)(align=“left”)>/

so the result will be something like <img … align=“left” style=“padding: 0 5px 5px 0”>

Thanks!
Cheers,
Greg

Need to extract ‘align=“…”’ from <IMG… tag

Is it possible via the regex?
Thanks!

Kind Regards,
Greg Bialowas

You shouldn’t be using align at all, can you use a style instead with your editor? If not, get a better one :stuck_out_tongue:

That’s the whole point:
I want to extract the align attribute and add css instead.
NicEdit is a pretty damn good editor, too :wink:

The whole point is that you’re editor is forcing you to use regex to fix what it can’t do? I’m missing it :S

Why put the CSS in the tag’s style attribute? It could easily go in a stylesheet.


img[align="left"]    {padding: 0 5px 5px 0;}

P.S. all editors are garbage. You could write it yourself or use a non-HTML markup like Markdown.

Thaaaaaank You! Works beautifully!

It’s for the client, she needs a wysiwyg

Thanks again for the css tip, I didn’t even of this solution, I set my mind completely on regex. sigh…

Cheers,
Greg