Remove inline style with preg replace


hope i can post preg replace question in this php forum

i want to replace inline style with preg replace

$desc = '<table style="PADDING-BOTTOM: 0px; BORDER-RIGHT-WIDTH: 0px; WIDOWS: 2; TEXT-TRANSFORM: none; BACKGROUND-COLOR: rgb(255,255,255); TEXT-INDENT: 0px; MARGIN: 0px; PADDING-LEFT: 0px; WIDTH: 476px; PADDING-RIGHT: 0px; FONT: 11px/11px Arial, sans-seirf; WHITE-SPACE: normal; ORPHANS: 2; BORDER-BOTTOM-WIDTH: 0px; LETTER-SPACING: normal; COLOR: rgb(0,0,0); VERTICAL-ALIGN: baseline; BORDER-TOP: rgb(227,227,227) 1px solid; BORDER-LEFT-WIDTH: 0px; WORD-SPACING: 0px; PADDING-TOP: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" cellspacing="0">
            <th style="BACKGROUND-IMAGE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 1px; TEXT-TRANSFORM: uppercase; BACKGROUND-COLOR: rgb(246,246,246); MARGIN: 0px; PADDING-LEFT: 6px; WIDTH: 55px; BACKGROUND-ATTACHMENT: scroll; PADDING-RIGHT: 6px; FONT: 13px Oswald; BACKGROUND-POSITION: 0px 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; COLOR: rgb(178,63,51); VERTICAL-ALIGN: top; BORDER-LEFT-WIDTH: 0px; BORDER-RIGHT: medium none; PADDING-TOP: 1px" rowspan="4" scope="row">GENERAL</th>';

this is my preg code

$full_description = preg_replace('/style[^>]*/', '', $desc);
echo $full_description;

But the above preg code remove other attributes like “cellpadding, rowspan, colspan etc”

i want just to remove the style attribute


It must like here

$full_description = preg_replace('/style=\\"[^\\"]*/', '', $desc); 
echo $full_description;
1 Like

@akalanez; you forgot the closing " so it will replace style=“…” with " instead of removing it altogether.

$full_description = preg_replace('/style=\\"[^\\"]*\\"/', '', $desc); 
echo $full_description;  


1 Like

thanks scallio

works perfect


1 Like