Jump to content

PHP striptags allowing only title attribute.


Mudsaf

Recommended Posts

Well if user enters tags example

$MyString = '<span style="display:block;" title="WADAP">MyText</span>';strip_tags ($MyString,"<span title>"); //So this would remove the style attribute.

Result should look like

<span title="WADAP">MyText</span>
Edited by Mudsaf
Link to comment
Share on other sites

I found tutorial for style, but didin't find method to remove ID and CLASS attributes.

preg_replace('/style=(["'])[^1]*?1/i', '', $item, -1);
Link to comment
Share on other sites

I see several examples if I do a search. If this is user-generated HTML then it would be safer to load the HTML into a DOMDocument object and traverse through the nodes, where you can call removeAttribute to remove attributes for a node and then write out the final HTML. DOMDocument will behave better for broken HTML.

 

http://docs.php.net/manual/en/domdocument.loadhtml.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...