function to remove tags and/or tag attributes as I specify, and add tag attributes as I specify
Posted on 2006-11-20
I need a php function that can allow me to remove tags & tag attributes into/from a string.
I should give the function the string to be processed and an array containing the attributes and attribute properties to be removed.
For example when a tag (<a>) has a style attribute, I want to be able to remove specific style properties for example border & padding, but leave the unspecified ones in tact - and if no attribute properties are specified, the entire attribute needs to be removed.
The function should also remove tags that I specify, including closing tags, and incorrectly opened / closed tags.
The function should work with xhtml & html
The second function should be able to add attributes to specified tags within the given string. For example: After I stripped all attributes from my <a> tag in the given string, I want to be able to add a rel attribute with for example a nofollow and an external attribute property.
All functions should automatically remove all double tags, hacking attempts, etc... for example it should remove all tags except those I specify as safe, similar to the php strip_tags function. It should check for things like these:
<!-- I want to put a <div>tag</div> -->
<!-- Or a carrot > -->
<![CDATA[what about this! It's to protect from HTML characters like <tag>, > and so on in XML, no?]]> -->
If I'm not exactly clear on everything, please let me know, but I think this is pretty clear. The functions will be used primarily to process blog replies.
Thanks for any help!