dpacmittal Posted May 31, 2009 Share Posted May 31, 2009 I have retrieved html contents using CURL. I successfully retrieved all the contents between the needed form tags. It has some table tags (<tr><td><th>) and many other tags. I just want to retrieve all <input> tags, <select> tags, and <textarea> tags. Whats the regex I should use to clear all unneeded tags? Link to comment https://forums.phpfreaks.com/topic/160380-how-to-clear-all-html-tags-except-some-from-html-file-using-regex/ Share on other sites More sharing options...
.josh Posted May 31, 2009 Share Posted May 31, 2009 example: remove all tr tags: $content = preg_replace('~</?tr[^>]*>~i','',$content); note: that does not remove content between the tags. Link to comment https://forums.phpfreaks.com/topic/160380-how-to-clear-all-html-tags-except-some-from-html-file-using-regex/#findComment-846365 Share on other sites More sharing options...
dpacmittal Posted May 31, 2009 Author Share Posted May 31, 2009 example: remove all tr tags: $content = preg_replace('~</?tr[^>]*>~i','',$content); note: that does not remove content between the tags. Thanks.. will try it. Can't we do like just remove all tags except few. I know some regex but not so complex. I know we can add ^ which means "NOT". Can't we do something like that? Link to comment https://forums.phpfreaks.com/topic/160380-how-to-clear-all-html-tags-except-some-from-html-file-using-regex/#findComment-846374 Share on other sites More sharing options...
rea|and Posted June 3, 2009 Share Posted June 3, 2009 Thanks.. will try it. Can't we do like just remove all tags except few. I know some regex but not so complex. I know we can add ^ which means "NOT". Can't we do something like that? Have you tried strip_tags? As second parameter it takes the allowable tags. Anyways if you want do it with regex try this one (I've modified Crayon Violent regexp adding a negative lookahead assertion): $content=preg_replace('/<\/?(?!input|textarea|select)[^>]*>/','',$content); In some cases it could have problems (html code within html comments ... casually I tried it against a page that had it). Link to comment https://forums.phpfreaks.com/topic/160380-how-to-clear-all-html-tags-except-some-from-html-file-using-regex/#findComment-848509 Share on other sites More sharing options...
dpacmittal Posted June 4, 2009 Author Share Posted June 4, 2009 Thanks for this. Link to comment https://forums.phpfreaks.com/topic/160380-how-to-clear-all-html-tags-except-some-from-html-file-using-regex/#findComment-849343 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.