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

Untested… totally guessing. LMK.



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

<!DOCTYPE html>
<html lang="en">
   <meta charset="utf-8"/>
      <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>
   <script type="text/javascript" src="jquery.js"></script>
   <script type="text/javascript">



A bit more specific:

$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.