jjk2 Posted January 29, 2009 Share Posted January 29, 2009 so i downloaded an html page. is there anywy of stripping all html tags except h1 h2 h3 <strong> <b> and <a> Quote Link to comment https://forums.phpfreaks.com/topic/143019-php-library-to-rip-all-html-tags-except-a-h1-and/ Share on other sites More sharing options...
.josh Posted January 29, 2009 Share Posted January 29, 2009 strip_tags Quote Link to comment https://forums.phpfreaks.com/topic/143019-php-library-to-rip-all-html-tags-except-a-h1-and/#findComment-749966 Share on other sites More sharing options...
jjk2 Posted January 29, 2009 Author Share Posted January 29, 2009 so should i first use smoething to tidy up the html page first, make sure no broken tags exist, and then use strip_tags. will strip_tags work for an array containing the html page. Quote Link to comment https://forums.phpfreaks.com/topic/143019-php-library-to-rip-all-html-tags-except-a-h1-and/#findComment-749976 Share on other sites More sharing options...
premiso Posted January 29, 2009 Share Posted January 29, 2009 so should i first use smoething to tidy up the html page first, make sure no broken tags exist, and then use strip_tags. will strip_tags work for an array containing the html page. implode the array and it will. No you do not need to tidy up the html. Just make sure the ones you want allowed are in the 2nd parameter like shown in the manual. Quote Link to comment https://forums.phpfreaks.com/topic/143019-php-library-to-rip-all-html-tags-except-a-h1-and/#findComment-749979 Share on other sites More sharing options...
.josh Posted January 29, 2009 Share Posted January 29, 2009 you will have to make sure no broken tags exist, yes. strip_tags does not validate html, so it may produce unexpected results. Quote Link to comment https://forums.phpfreaks.com/topic/143019-php-library-to-rip-all-html-tags-except-a-h1-and/#findComment-749986 Share on other sites More sharing options...
jjk2 Posted January 30, 2009 Author Share Posted January 30, 2009 anyway to fix bad html tags ? and then strip_tags from it ? Quote Link to comment https://forums.phpfreaks.com/topic/143019-php-library-to-rip-all-html-tags-except-a-h1-and/#findComment-750481 Share on other sites More sharing options...
Daniel0 Posted January 30, 2009 Share Posted January 30, 2009 You can do this: $text = preg_replace('#</?(h[1-3]|b|strong|a)[^>]*>#', '', $html); That'll work with invalid HTML as well. Quote Link to comment https://forums.phpfreaks.com/topic/143019-php-library-to-rip-all-html-tags-except-a-h1-and/#findComment-750488 Share on other sites More sharing options...
.josh Posted January 30, 2009 Share Posted January 30, 2009 he wants to remove all tags except those tags. Quote Link to comment https://forums.phpfreaks.com/topic/143019-php-library-to-rip-all-html-tags-except-a-h1-and/#findComment-750617 Share on other sites More sharing options...
Daniel0 Posted January 30, 2009 Share Posted January 30, 2009 Alright then try with a negative lookahead: $text = preg_replace('#</?(?!h[1-3]|b|strong|a)[^>]*>#', '', $html); Quote Link to comment https://forums.phpfreaks.com/topic/143019-php-library-to-rip-all-html-tags-except-a-h1-and/#findComment-750626 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.