PHP how to remove rgb(255, 255, 0); in a string

I want to remove the rgb(number, number, number); in my string using php but I’m not sure what to use

<p>asd <span style="background-color: rgb(255, 255, 0);">a12</span>asd<span style="font-size: 8px;"></span></p>

that the string contains

One of the ways I know is preg_replace but I’m not sure how to remove it without affecting the other number

$p = preg_replace(' /[,0-9]+/ ', '', $p);

that is what I use but it will remove the 12 in the span tag , any recommendation would help, Thanks

the output that i needed is this

 <p>asd <span>a12</span>asd<span style="font-size: 8px;"></span></p>

that the string contains

I use str_replace for the front part

$p = str_replace(' background-color:', '', $p);
$p = str_replace(' style=','', $p);

I’m thinking of using preg_replace to catch all the different values in the rgb() but I don’t know how to do it. any help would be great, thanks.

>Solution :

  1. Use PHP regex: preg_replace('/style="background-color: rgba?\(\d+, \d+, \d+(, \d+)?\);?"/', '', $p);works only if attribute always in same format. E.g. remove ; or add additional style, and regex will fail, because HTML is not regular language
  2. Use CSS rules: p > span {background-color: transparent; !important;}

Leave a Reply