Regexp to add attribute in any xml tags
        Posted  
        
            by katsuo11
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by katsuo11
        
        
        
        Published on 2009-08-31T13:01:11Z
        Indexed on 
            2010/04/30
            8:07 UTC
        
        
        Read the original article
        Hit count: 294
        
Hello,
I have well-formed xml documents into string variables. I want to use preg_replace to add a defined attribute to every xml tags.
For example replace:
<tag1>
<tag2> some text </tag2>
</tag1>
by:
<tag1 attr="myAttr">
<tag2 attr="myAttr"> some text </tag2>
</tag1>
So I basically need the regex expression to find any start tags and add my attribute, but I'm a complete regex noob.
Thanks, kats
© Stack Overflow or respective owner