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
Solved

Strip style and class tags

Posted on 2010-09-20
10
353 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
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 28

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 28

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
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.

828 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