Log in

View Full Version : how to remove attribute on an element?



ampera
12-28-2010, 08:17 PM
Please help !!
How to remove attribute (href, style, id, class, name, title, etc.) on the elements <a> <p> <div> using php ?

For example:
<p style="font:10px arial;background-color:#f3f3f3; etc.." title=".." name=".."> remove attribute </ p>

Output:
<p> remove attribute </ p>

Beverleyh
12-28-2010, 08:26 PM
I've had success with this function: http://www.php.net/manual/en/function.strip-tags.php#96483

bluewalrus
12-28-2010, 08:29 PM
The easiest way I think is


$code = preg_replace('/<(div).*?>|<(a).*?>|<(p).*?>/s', "<$1$2$3>", $code);

The '.*?' says find anything until (. means any single charachter) (* means find all preceding characters in this case any so it is infinite until....) (? means find the first occurrence of the trailing character in this case >) and the '|' is or.

ampera
12-28-2010, 08:57 PM
@bluewalrus : very simple .. thanks for the solution ...

VijayKanta
12-29-2010, 05:21 AM
Thanks bluewalrus for the detailed explanation. You even gave the explanation for the regular expression. Top stuff!

nextdownload
01-06-2011, 06:30 PM
bluewalrus thanks, I'm looking for long time after this regular expression.