php - str replace with mask text -


i have text need delete fron code , using variants 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 create 1 str_replace function delete text wia mask?

preg_replace() function need.

assuming want identify , replace hex representations of colors, code along lines:

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

the i pcre modifier tells preg_replace ignore character cases.

it identifies , replaces when color code contains 6 hex digits. in order make identify color codes using 3-digit rgb notation need change [0-9a-f]{6} part of regexp [0-9a-f]{3}([0-9a-f]{3})?. or use simpler expression matches color codes between 3 , 6 digits: [0-9a-f]{3,6}

you put \s* after colon (:) make match when 1 or more whitespaces present after background-color: part.

the updated code is:

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

however, if want match colors can put color codes separated | instead:

$answer = preg_replace(     '/<td style="background-color:\s*#(80f9cc|e1e3e4|d1d3d4|73f6ab|3ceb88);">/i',     '',     $answer ); 

short explanation of regular expression pieces:

<td style="background-color:    # plain text, matches if exact \s*                             # matches 0 or more (*) space characters (\s) #                               # plain text, matches 1 '#' (                               # start of group, doesn't match      80f9cc                         # matches '80f9cc'     |e1e3e4                         # or (|) 'e1e3e4'     |d1d3d4                         # or 'd1d3d4'     |73f6ab                         # or ...     |3ceb88                         # or ... )                               # end of group     ;">                             # plain text; matches text 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -