Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Strip style and class tags

Posted on 2010-09-20
10
Medium Priority
?
362 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.
Suggested Courses

636 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