Current location: Hot Scripts Forums » Programming Languages » PHP » Removing html tag attributes


Removing html tag attributes

Reply
  #1 (permalink)  
Old 04-01-08, 08:56 AM
d*d d*d is offline
Newbie Coder
 
Join Date: May 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Removing html tag attributes

anyone know a good way to do this? got a CMS which is accepting style attributes for the <p> tag, which then override my css styles on the site. Need to keep the <p> tag just remove all it's attributes;

ie
<p style="margin: 0px; font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; line-height: normal; font-size-adjust: none; font-stretch: normal">

becomes

<p>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 04-01-08, 09:11 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

preg_replace('/<p[^>]+>/i','<p>',$content); 

Something like that should do it. put your content into the $content variable, and then run the preg_replace function on it

Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 04-01-08, 09:57 AM
d*d d*d is offline
Newbie Coder
 
Join Date: May 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Cheers figured it'd be some sort of regular expression - thought it may be more complex than that though. I was hoping there may have been a command to achieve it similar to strip_tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 04-01-08, 10:17 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Not that I am aware of. If you want to do it for all tags, a simple
PHP Code:

preg_replace('/<\s*(\w+)[^>]+>/i','<$1>',$content); 

will strip anything after the initial tag

EDIT: In the orignial code, it will match <param> and <pre> so you need to check for them. change the pattern to
Code:
'/<p\b[^>]*+>/i'
Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName

Last edited by Jay6390; 04-01-08 at 10:37 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 01-26-09, 06:49 PM
nachenko's Avatar
nachenko nachenko is offline
New Member
 
Join Date: Jan 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
oh, yeah, thanks a lot, this saved my ***.

BTW, the expression had a quirk. It was cutting the last letter from all tags that had no attributes, but this problem has a very easy solution:

Code:
preg_replace('/<\s*(\w+) [^>]+>/i', '<$1>',$content);
Notice i added a space between w+) and [^. I'm not sure how good this solution is, but it works for me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 01-27-09, 03:58 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
If you want to strip any tag, use the strip_tags function, you don't need a regular expression
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 03-21-09, 01:30 AM
crodriguez1a crodriguez1a is offline
Newbie Coder
 
Join Date: Feb 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
leave some attributes intact

Could I leave some attributes in?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 03-21-09, 03:38 AM
lozanoerwin lozanoerwin is offline
New Member
 
Join Date: Mar 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
what is your CMS?
if its Joomla I think they can handle it perfectly with some add-ons and some other components.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
html tutoral thefrtman HTML/XHTML/XML 5 04-27-09 11:25 AM
Removing \ from HTML leeprice2006 PHP 2 08-30-07 11:41 AM
Can PHP place quotes around HTML attributes frdmsun PHP 1 08-17-04 03:12 AM
removing specified html tags from text memon PHP 1 01-15-04 09:56 PM


All times are GMT -5. The time now is 05:50 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.