tags:

views:

44

answers:

1

Hey,

I need to increase indentation of a formatted xml file to 4 spaces from 2 I tried using a regex powered XML formatter but it forgot to indent closing tags that were on a new line.

What options do I have, using preg_replace seems the best option, the search regex would be something like /( {2}^ANYTHING_EXCEPT_SPACE)/ but that's as far as I can seem to get.

Thanks, Xeross

+2  A: 
preg_replace('/^[ ]+(?=<)/m','$0$0',$string);
Wrikken
Thanks, something else I'm wondering about is why a tag that has a newline inbetween the opening and closing tags and nothing else (Except whitespace) gets formatted wrongly: http://pastebin.com/c3C6faNE
Xeross
Oh yes and you have 1 closing parentheses too many
Xeross
@Xeross: Aha, it did also match '\n\n{spaces}' due to the `\s`, altered it slightly so it should only match spaces, not other whitespace (`\n\r\t`)
Wrikken
Thanks, by the way, is there some kind of tool that explains regex's because it'd be nice to understand how this regex actually works
Xeross
RegexBuddy is a great one, however not free. There may be satisfactory free regexparsers out there.
Wrikken
Thanks, seems to work pretty well :)
Xeross