Jump to content

RegEx to add inline css (style tag) to link tag (<a>)


everisk

Recommended Posts

Hello,

 

I dont understand a little about RegEx but cannot really understand how to use all the funtions :(. I need to convert link tag <a href ....> that DOES NOT already have the css in them. Such as <a href="http://www.google.com" style="font-size: 11px;" other properties> should NOT matched but <a href="http://www.google.com" .. other properties> should match and a style="..." should be added.

 

I understand that I need to use preg_replace for that but dont know how to use it :(

 

Any help would be much appreciated. Thanks!

<?php
$data = <<<DATA
I dont understand a little about RegEx but cannot really understand how to use all the funtions . I need to convert link tag <a href ....> that DOES NOT already have the css in them. Such as <a href="http://www.google.com" style="font-size: 11px;" other properties> should NOT matched but <a href="http://www.google.com" .. other properties> should match and a style="..." should be added.
DATA;

echo htmlspecialchars(preg_replace('/<a((??!style=)[^>])+)>/', '<a style="xyz" $1>', $data));
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.