Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$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;}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading