Add !important to all css attributes for a:*{}

Using regex I want to add !important to the end of each attributes for all "a" items.

For example,
   a:link {color: red}
Should become:
   a:link {color: red !important}

and

#divID p, #divID a:link, #divID a:hover {color: red; text-decoration: none; font-weight: bold;}

Should become:

#divID p, #divID a:link, #divID a:hover {color: red !important; text-decoration: none !important; font-weight: bold !important;}

This should only be done if  
       a:
is before { and }


a:link { color: white  font-size: 14px; font-family: Helvetica; text-decoration: none }
A:visited {
	color: white  
	font-size: 14px; 
	font-family: Helvetica; 
	text-decoration: none  }
A:active { color: #f4cd03  font-size: 14px; font-family: Helvetica; text-decoration: none  !important }
A:hover {
color: #f4cd03  font-size: 14px; font-family: Helvetica; 
text-decoration: none  }

Open in new window

LVL 16
hankknightAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddrudikCommented:

<pre>
<?php
function repfunc($match){
  return $match[1].preg_replace('/ {2,}/',' ',preg_replace('/\s*!important(?=\s*!important)/i','',preg_replace('/(?=;|$)/i',' !important',$match[2]))).$match[3];
}
$content = 'a:link { color: white;  font-size: 14px; font-family: Helvetica; text-decoration: none }
A:visited {
	color: white;
	font-size: 14px; 
	font-family: Helvetica; 
	text-decoration: none  }
A:active { color: #f4cd03;  font-size: 14px; font-family: Helvetica; text-decoration: none  !important }
A:hover {
color: #f4cd03;  font-size: 14px; font-family: Helvetica; 
text-decoration: none  }
';
echo "$content<hr>";
$content=preg_replace_callback('/(a:[^{]*\{)([^}]*)(\})/i','repfunc',$content);
echo $content;
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ddrudikCommented:
Thanks for the question and the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.