Regular expression remove colors?

How can I remove from string some like this please need an 3 expressions

color=#FFCCCC //could have spaces
#333; // could be 3 or 6 could have spaces
color:#333; // could be 3 or 6 could have spaces
/(color=|color:)?#([\d|\w]{3,6};?)/gi

Untested… totally guessing. LMK.

V/r,

:slight_smile:

@letsforum I did manage to test it, in JavaScript. I’m sure you can alter it for PHP.

<!DOCTYPE html>
<html lang="en">
   <head>
   <meta charset="utf-8"/>
   </head>
   <body>
      <div id="one">This should disappear color=#FFCCCC</div>
      <div id="oneb"></div>
      <div id="two">So should this #333;</div>
      <div id="twob"></div>
      <div id="three">.. and this, too color:#333;</div>
      <div id="threeb"></div>
   </body>
   <script type="text/javascript" src="jquery.js"></script>
   <script type="text/javascript">
   $(document).ready(function(){
      $('#oneb').html($('#one').html().replace(/(color=|color:)?#([\d|\w]{3,6};?)/gi,''));
      $('#twob').html($('#two').html().replace(/(color=|color:)?#([\d|\w]{3,6};?)/gi,''));
      $('#threeb').html($('#three').html().replace(/(color=|color:)?#([\d|\w]{3,6};?)/gi,''));
   });
  </script>
</html>

V/r,

:slight_smile:

A bit more specific:

<?php
$pattern = "~(color\s?[=:]\s?)?#[\da-f]{3}([\da-f]{3})?;?~i";

Optionally {
The Word ‘color’ followed by an optional space, followed by either a colon or an equals sign, followed by an optional space. }
A # symbol.
3 digits or the letters A-F.
Optionally, 3 more digits or the letters A-F.
Optionally, a semicolon.
Case Insensitive Search.

(G is not a pattern modifier in PHP.)

\d\w {3,6} is not precise enough for this, as that would allow
#TEPG to match, which is not legal hex-color declaration. (could screw up anchor tags, etc… though that said, if you start an anchor tag with #abcdef, this pattern will still find it…)

1 Like

Guys thanks so much all of yo. I love you all thanks bug time!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.