• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

Strip style and class tags

Hello,

I have $description and I would like to strip out all of the style and class tags with the allowed tags.

I tried this but no luck

<?
$desc = strip_tags($description,"<b></b><br><br /><p></p><strong></strong>");
echo preg_replace('#(<[a-z ]*)(style=("|\')(.*?)("|\'))([a-z ]*>)#', '\\1\\6', $desc);
?>
0
movieprodw
Asked:
movieprodw
  • 3
  • 3
  • 3
  • +1
1 Solution
 
sammySeltzerCommented:
Try this:


$desc = strip_tags($descr\, $allowed);
0
 
zappafan2k2Commented:
can you please give an example of $description and your desired output?
0
 
Terry WoodsIT GuruCommented:
Try this perhaps:

$desc = strip_tags($description,"<b><br><br /><p><strong>");
echo preg_replace('#(<[a-z ]*)style=(?:"|\').*?(?:"|\')([a-z ]*>)#','$1$2',$desc);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
movieprodwAuthor Commented:
Terry that did not work.

Here is an example

<p class="MsoNormal" style="margin: 0in 0in 0pt;"><strong>2010 BMW M6 Options include:</strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Alpine White</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sepang Merino Leather</p>

<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Universal Garage Door Opener</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Comfort Access System</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Soft Close Automatic Doors</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Carbon Fiber Black Trim</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Auto Dimming Mirrors</p>

<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Ski Bag</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Park Distance Control</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Adaptive Light Control</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; iPod &amp; USB Adaptor</p>

<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Navigation System</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BMW Assist</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HD Radio</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Satellite Radio</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in;">-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Enhanced Premium Sound</p>
0
 
movieprodwAuthor Commented:
My disired output is no class or style tags in p or b tags
0
 
sammySeltzerCommented:
did you try mine or did you "assume" it won't work?

Mine strips desc of ALL tags
0
 
movieprodwAuthor Commented:
Hello Sammy,

I did not know how to implement it, should I define the allowed and descr?

$desc = strip_tags($descr\, $allowed);
0
 
sammySeltzerCommented:
sorry, I was typing and typed too fast.

my apology


should be:

$desc = strip_tags($desc, '');
0
 
Terry WoodsIT GuruCommented:
To keep the pattern somewhat more maintainable, I recommend running it twice rather than trying to adjust the pattern to allow the case:
<b attr=foo style="blah" attr="bar" class="asdf">

This will hopefully work for you:
$desc = strip_tags($description,"<b><br><br /><p><strong>");
$pattern = '#(<[a-z ]*)(?:style|class)\s*=\s*(?:"[^"]*"|\'[^\']*\')([^>]*>)#';
$replacement = '$1$2';
$result = preg_replace($pattern,$replacement,$desc);
$result = preg_replace($pattern,$replacement,$result);
0
 
Terry WoodsIT GuruCommented:
And to output the result:

$desc = strip_tags($description,"<b><br><br /><p><strong>");
$pattern = '#(<[a-z ]*)(?:style|class)\s*=\s*(?:"[^"]*"|\'[^\']*\')([^>]*>)#';
$replacement = '$1$2';
$result = preg_replace($pattern,$replacement,$desc);
$result = preg_replace($pattern,$replacement,$result);
echo $result;
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now