Solved

Strip style and class tags

Posted on 2010-09-20
10
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 33721736
Try this:


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

Expert Comment

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

Expert Comment

by:Terry Woods
ID: 33721751
Try this perhaps:

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

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 1

Author Comment

by:movieprodw
ID: 33721777
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
ID: 33721782
My disired output is no class or style tags in p or b tags
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 33721786
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
ID: 33721789
Hello Sammy,

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

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

Expert Comment

by:sammySeltzer
ID: 33721792
sorry, I was typing and typed too fast.

my apology


should be:

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

Expert Comment

by:Terry Woods
ID: 33721797
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
ID: 33721801
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 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