0

I have some text that i need to delete fron code and i using much variants like this:

$answer = str_replace('<td style="background-color:#80F9CC;">', '', $answer);
$answer = str_replace('<td style="background-color:#E1E3E4;">', '', $answer);
$answer = str_replace('<td style="background-color:#D1D3D4;">', '', $answer);
$answer = str_replace('<td style="background-color:#73F6AB;">', '', $answer);
$answer = str_replace('<td style="background-color:#3CEB88;">', '', $answer);

Is there way to create one str_replace function that will delete this text with some regular expression?

1
  • 2
    Im just wondering about your end goal here.. if you replace your opening <td> with a blank '' then you are left with a close </td> tag without its opening tag, leading to bad HTML. Is this really what you want? Commented Jan 27, 2016 at 15:11

2 Answers 2

7

preg_replace() is the function you need.

Assuming you want to identify and replace the hex representations of colors, the code is something along the lines:

$answer = preg_replace('/<td style="background-color:#[0-9A-F]{6};">/i', '', $answer);

The i PCRE modifier tells preg_replace to ignore character cases.

It identifies and replaces only when the color code contains exactly 6 hex digits. In order to make it identify color codes using the 3-digit RGB notation you need to change the [0-9A-F]{6} part of the regexp to [0-9A-F]{3}([0-9A-F]{3})?. Or use a simpler expression that matches all color codes between 3 and 6 digits: [0-9A-F]{3,6}

You could also put \s* after the colon (:) to make it also match when one or more whitespaces are present after the background-color: part.

The updated code is:

$answer = preg_replace(
    '/<td style="background-color:\s*#[0-9A-F]{3,6};">/i', '', $answer
);

However, if you want to match only some colors then you can put the color codes separated by | instead:

$answer = preg_replace(
    '/<td style="background-color:\s*#(80F9CC|E1E3E4|D1D3D4|73F6AB|3CEB88);">/i',
    '',
    $answer
);

Short explanation of the regular expression pieces:

<td style="background-color:    # plain text, matches if exact
\s*                             # matches zero or more (*) space characters (\s)
#                               # plain text, matches exactly one '#'
(                               # start of a group, doesn't match anything
     80F9CC                         # matches '80F9CC'
    |E1E3E4                         # or (|) 'E1E3E4'
    |D1D3D4                         # or 'D1D3D4'
    |73F6AB                         # or ...
    |3CEB88                         # or ...
)                               # end of the group    
;">                             # plain text; matches exactly this text
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! Perfect solution:)
Does this match the specific colors he has? or is it all colors with 6/3 digits?
It matches all the colors.
Gotcha, so I guess this would work perfectly if you want to remove all colors from the attributes, but not specific colors.
Now its perfect. Awesome breakdown.
3

str_replace() can take an array as an argument:

$replace_array = [
    '<td style="background-color:#80F9CC;">',
    '<td style="background-color:#E1E3E4;">',
    '<td style="background-color:#73F6AB;">',
    '<td style="background-color:#3CEB88;">',
];

$answer = str_replace($replace_array, '', $answer);

or you could use a preg_replace() instead

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.