?
Solved

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

Posted on 2008-11-06
2
Medium Priority
?
447 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:hankknight
  • 2
2 Comments
 
LVL 27

Accepted Solution

by:
ddrudik earned 2000 total points
ID: 22898351

<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
 
LVL 27

Expert Comment

by:ddrudik
ID: 22898497
Thanks for the question and the points.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question