?
Solved

function to remove tags and/or tag attributes as I specify, and add tag attributes as I specify

Posted on 2006-11-20
8
Medium Priority
?
192 Views
Last Modified: 2013-11-19
Hi,

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!
0
Comment
Question by:nelwa
[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
  • 4
  • 2
8 Comments
 
LVL 6

Expert Comment

by:aescnt
ID: 17986010
Hi,

If you want to clean up 3rd party HTML (like for blog comments), you'll love this project: http://pixel-apes.com/safehtml/

I don't think it does nofollow or style-stripping, but it does some essential things to HTML to make it 'safe.' (XSS issues and such.) After passing your 3rd party HTML through it, I'm sure you can use regex afterwards to add nofollow and remove styles.
0
 
LVL 1

Author Comment

by:nelwa
ID: 17986075
ok, yes so that function will work to remove xss and such, but now I need something to add attributes and attribute properties to certain tags...
0
 
LVL 1

Author Comment

by:nelwa
ID: 18002449
ok, so is nobody going to help me with this??
0
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 1

Author Comment

by:nelwa
ID: 18002506
All I need now is a function to remove all tag attributes, and another function that can add attributes with properties to the tags I specify... any help appreciated.

Thanks
0
 
LVL 6

Expert Comment

by:aescnt
ID: 18002516
Sorry, the code required for that is rather too complicated to write quickly.
0
 
LVL 1

Author Comment

by:nelwa
ID: 18002840
I have found a solution to my problem: http://cyberai.com/inputfilter/

Thanks
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 18256196
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

801 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