11

I can't quite figure it out, I'm looking for some code that will add an attribute to an HTML element.

For example lets say I have a string with an <a> in it, and that <a> needs an attribute added to it, so <a> gets added style="xxxx:yyyy;". How would you go about doing this?

Ideally it would add any attribute to any tag.

miken32
  • 39,644
  • 15
  • 91
  • 133
CafeHey
  • 5,607
  • 17
  • 78
  • 137
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Mike Axiak Oct 20 '10 at 23:17
  • I actually wrote a php function to do that... wanted to search for all hyperlinks in a block of text, and created a target='blank' attribute, or changed the existing one to be target='blank'. It was a pretty complex process, regex matching was just a small part. – Sam Dufel Oct 20 '10 at 23:21

2 Answers2

21

It's been said a million times. Don't use regex's for HTML parsing.

    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $x = new DOMXPath($dom);

    foreach($x->query("//a") as $node)
    {   
        $node->setAttribute("style","xxxx");
    }
    $newHtml = $dom->saveHtml()
Byron Whitlock
  • 51,185
  • 28
  • 118
  • 166
  • 4
    How would you prevent `DOMDocument()` adding `` wrapper around the given tag? – Sisir Feb 10 '14 at 05:11
  • 1
    Use another language. Joke aside, since DOMDocument is essentially crap you have to do some str_replace on the document to remove anything it added – Ms01 Sep 29 '14 at 07:32
  • You can use `$node->c14n()` to get the canonical HTML for the node. It won't wrap in `` tags. – Byron Whitlock Oct 16 '14 at 18:32
  • There are some quite good reasons to use Regex to parse HTML... Though in this case you are right, the OP should not aim for regex but for some other solution. – Philipp May 02 '18 at 10:35
10

Here is using regex:

  $result = preg_replace('/(<a\b[^><]*)>/i', '$1 style="xxxx:yyyy;">', $str);

but Regex cannot parse malformed HTML documents.

Vantomex
  • 2,177
  • 5
  • 19
  • 22