ryancooper Posted April 7, 2011 Share Posted April 7, 2011 Okay, i wasnt sure what section to post in but im leaning toward this one so here it is. I am trying to find the most efficient way to remove not all HTML but some, strip_tags lets you define allowable tags, i need to only remove certain ones, the problem is if i have <-- or << then strip_tags removes them with the rest of the HTML, Is there a way to only take tags that were opened and closed? Or in other words valid HTML? Or would i have to create a regex for every common HTML tag to have it stripped on its own? I'm new to PHP so the only work around ive been able to use is to str_replace every group of characters into a number, then strip_tags, then the number back into the associated characters... i realize this is a rigged work around and thats why im looking for something more efficient especially since any undefined groups get pulled. One option which im not even sure is possible would be to match any HTML and if its a letter following the bracket have it stripped the only exception would be to also taking out / in closing brackets, this way <a herf would be stripped <b> </b> would both be stripped, so if the bracket is followed by any non A-Z character besides / it will be stripped, but if any bracket is followed by another bracket or various other symbols that arent used it will be left alone. Quote Link to comment https://forums.phpfreaks.com/topic/232926-help-with-either-regex-or-striptags/ Share on other sites More sharing options...
ryancooper Posted April 7, 2011 Author Share Posted April 7, 2011 The main issue is with strip_tags if i post. Grrr <-- Yeah i was super frustrated there. <b>I'm better now.</b> strip_tags removes everything but Grr since it sees the <-- as a comment bracket and strips everything from there on in... Quote Link to comment https://forums.phpfreaks.com/topic/232926-help-with-either-regex-or-striptags/#findComment-1198000 Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 Your post was a little confusing... and I feel like this code could break at any moment, but here's what I came up with: $subject = preg_replace('#<[^!]+>#U', '', $subject); If you still want allowable tags.. well.. good luck. Quote Link to comment https://forums.phpfreaks.com/topic/232926-help-with-either-regex-or-striptags/#findComment-1198568 Share on other sites More sharing options...
BizLab Posted April 15, 2011 Share Posted April 15, 2011 Do you absolutely have to have the <-- in there or can you use the < entity instead... just a thought. You don't want to allow literals like this one on you site anyway - for various reasons Quote Link to comment https://forums.phpfreaks.com/topic/232926-help-with-either-regex-or-striptags/#findComment-1202131 Share on other sites More sharing options...
BizLab Posted April 15, 2011 Share Posted April 15, 2011 what i'm saying is that you can str_replace('<', '<'); str_replace('-', '–'); and save those entities for display, all while being safe Quote Link to comment https://forums.phpfreaks.com/topic/232926-help-with-either-regex-or-striptags/#findComment-1202133 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.