3

I need to search & replace inside HTML tags, CSS that was inlined, in order to avoid using the style="" attribute inline.

I.e. replace something that looks like this:

<table border="0" cellpadding="0" cellspacing="0" width="100%"  style="font-family: Helvetica;line-height: 100%;margin-top: 20px; text-align: left;vertical-align: bottom;color: #202020">

into something like that:

<table border="0" cellpadding="0" cellspacing="0" width="100%" font-family="Helvetica" line-height="100%" margin-top="20px" text-align="left" vertical-align="bottom" color="#202020">

Does someone know the regex for search & replace I would have to write in order to do that?

Thanks.

2
  • I don't think you can do that in a single pass. I'd do this with preg_replace_callback in PHP. Start with style="(.+?)". Also no way to know if attributes are valid going this route.. Commented Jul 13, 2015 at 18:37
  • 1
    What are you trying to do? A font-family attribute on a table tag won't do anything at all. Commented Jul 13, 2015 at 19:45

2 Answers 2

5

Use this regex replacement:

(?:\G(?!^)|\bstyle=")([^:]*):\s*([^;]*)[;"](?=[^>]*>)

Replace with (mind the space at the end):

$1="$2" 

Here is a demo

EXPLANATION

  • (?:\G(?!^)|\bstyle=") - A boundary where we'll start our matching. The boundary is the end of the previous match (\G(?!^)) or style=" (due to \bstyle=").
  • ([^:]*) - The 1st capturing group that holds a sequence of 0 or more characters other than :
  • : - a literal :
  • \s* - 0 or more whitespace
  • ([^;]*) - The 2nd capturing group that holds a sequence of 0 or more characters other than ;
  • [;"] - Either a ; or "
  • (?=[^>]*>) - We check the ending boundary to make sure we are inside a closing tag.
Sign up to request clarification or add additional context in comments.

4 Comments

I added explanations.
thanks but your regex fails when there 2 style="some css style" in the same string
@eric: Please provide some sample input string (better as a regex101.com link). Just guessing: maybe (?:\G(?!^)|\bstyle=")([^:>]*):\s*([^;>]*)[;"](?=[^>]*>) will work for you.
for example: ` <div><span style="line-height: 1.42857;">GDP Growth</span><br></div><div>&nbsp; in 2014</div><div>&nbsp; &nbsp; 2.9%</div> <div><span style="line-height: 1.42857;">GDP in 2014</span><br></div><div>&nbsp;S$390.0 Bil</div> how to raise funding for a bussiness`
5

You could do it like this:

  1. Match style="(.*?)" and save the captured group to a variable.
  2. On that variable, match ([a-zA-Z-]+):\s*(.*?)\s*; and replace it with {1}="{2}".
  3. Replace style=".*?" with the result of number 2.

2 Comments

style="(.*?)" was great, and in my case I used the same matching for replacing all these absolute height definitions with height='auto'. Very helpful!
Thank you so much, you've just saved me like 2 hours of work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.