You can use regular expression replacement with preg_replace()
. For example, to remove a bgcolor attribute that may or may not be there with a variable colour string:
$s = preg_replace('! bgcolor="#[0-9a-fA-F]{6}"!', '', $s);
But, as always, it's not recommended to use regular expressions to parse or process HTML. Lots of things can go wrong with this:
- 3 letter colour code;
- single quotes on attribute;
- no quotes on attribute;
- variable white space;
- uppercase attribute;
- colour names;
- rgb(N,N,N) and other legal formats;
- and so on.
And that's just for a limited subset of your problem.
It's far more robust to use DOM processing methods, of which there are several variants in PHP. See Parse HTML With PHP And DOM.