Solved

Strip style and class tags

Posted on 2010-09-20
10
348 Views
Last Modified: 2013-12-12
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
Comment
Question by:movieprodw
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
Comment Utility
Try this:


$desc = strip_tags($descr\, $allowed);
0
 
LVL 12

Expert Comment

by:zappafan2k2
Comment Utility
can you please give an example of $description and your desired output?
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
Try this perhaps:

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

Author Comment

by:movieprodw
Comment Utility
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
 
LVL 1

Author Comment

by:movieprodw
Comment Utility
My disired output is no class or style tags in p or b tags
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 28

Expert Comment

by:sammySeltzer
Comment Utility
did you try mine or did you "assume" it won't work?

Mine strips desc of ALL tags
0
 
LVL 1

Author Comment

by:movieprodw
Comment Utility
Hello Sammy,

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

$desc = strip_tags($descr\, $allowed);
0
 
LVL 28

Expert Comment

by:sammySeltzer
Comment Utility
sorry, I was typing and typed too fast.

my apology


should be:

$desc = strip_tags($desc, '');
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
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
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now