3

I'm looking to get only the style attribute and it's contents using regex in php.

For example

Input <img src="test.png" style="float:left;"/>

Desired Output style="float:left;"


What i've tried:

This seems to return the entire image, and i'm not sure why. I suck at regex.

$img = '<img src="test.png" style="float:left;"/>';
preg_match('/(<[^>]+) style=".*?"/i', $img, $match);

Returns:

    [0] => Array
    (
        [0] => <img src="test.png" style="float:left;"
        [1] => <img src="test.png"
    )

Anyone with any pointers?

Cheers.

Greg
  • 481
  • 1
  • 5
  • 21
  • `style` attribute? Please see [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/q/1732348/112968) on why RegEx is not always the right tool for the job. – knittl Aug 28 '13 at 14:25
  • Yeah I understand that, but in this situation i _have_ to use regex unfortunately. – Greg Aug 28 '13 at 14:26
  • Can you use javascript string operators? – Sunny R Gupta Aug 28 '13 at 14:29
  • @Greg: Why can't you _not_ use RegEx? I'm really interested in the reason. – knittl Aug 28 '13 at 14:33
  • @Knittl I'm working with some messy code I've inherited and don't have the time to adapt the rest of the code to use XML parsing – Greg Aug 28 '13 at 14:37
  • 1
    You don't have to adapt the rest (although it would be good). If the rest works as is, leave it alone. – knittl Aug 28 '13 at 14:38
  • Yeah I suppose you're right. I'll look into it, cheers. – Greg Aug 28 '13 at 14:39

1 Answers1

5

You have it almost correct, you just need to capture the part you want, by surrounding it in parenthesis. You're currently capturing the wrong part:

$img = '<img src="test.png" style="float:left;"/>';
preg_match('/<[^>]+ (style=".*?")/i', $img, $match);
$result = $match[1];

Demo

Note: This will work for simple inputs like the example <img> tag, but for anything more complex Regex is not powerful enough to parse HTML, since HTML is not a regular language. If you find that it's not powerful enough you can use DOMDocument, which is meant for this sort of thing.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • Brilliant, thanks! It's literally as simple as my example, so Regex works fine for now. If I had the choice I'd definitely use DOMDocument parsing. Thanks again! – Greg Aug 28 '13 at 14:38