anarchoi Posted June 12, 2009 Share Posted June 12, 2009 with strip_tags you can define some exceptions that will not be stripped out exemple: strip_tags($bouquin, '<font>'); I would like to remove all <font tags but keep only <font size=""> tags... is there a way to do that? Quote Link to comment https://forums.phpfreaks.com/topic/162002-strip_tags-help/ Share on other sites More sharing options...
.josh Posted June 12, 2009 Share Posted June 12, 2009 $string = '<font>some text</font> more text here.. <font size="">something here</font>'; $string = preg_replace('~<font>(.*?)</font>~','$1',$string); echo $string; Quote Link to comment https://forums.phpfreaks.com/topic/162002-strip_tags-help/#findComment-854803 Share on other sites More sharing options...
anarchoi Posted June 13, 2009 Author Share Posted June 13, 2009 i will explain better i have a text with a lot of font tags. Some are for the color, some are for the font, and all are different... I want to remove all FONT tags except the ones for the size (here again the size can vary) and is it possible to do this with strip_tags function? Quote Link to comment https://forums.phpfreaks.com/topic/162002-strip_tags-help/#findComment-854836 Share on other sites More sharing options...
ldougherty Posted June 13, 2009 Share Posted June 13, 2009 http://us3.php.net/manual/en/function.strip-tags.php In a simple answer no, the strip_tags will strip all tags unless stated as allowed and that tags can not include attributes. Quote Link to comment https://forums.phpfreaks.com/topic/162002-strip_tags-help/#findComment-854843 Share on other sites More sharing options...
.josh Posted June 13, 2009 Share Posted June 13, 2009 $string = '<font>some text</font> more text here.. <font size="">something here</font> blah blah <font >more blah</font> mmm <font style="something">blah</font> more blah <font style = "" size="3"> blah </font>'; $string = preg_replace('~<(font[^>]*size[^>]*)>(.*?)<(/font)>~','[:$1:]$2[:$3:]',$string); $string = preg_replace('~<font[^>]*>(.*?)</font>~','$1',$string); $string = preg_replace('~\[:(font[^\]]*size[^]]*):\](.*?)\[:(/font):\]~','<$1>$2<$3>',$string); so basically it first replaces all the font tags that have the size attribute in it with temporary tags. Then it removes all the rest of the font tags. Then it changes the ones with the size attribute back to real font tags. So yeah, that can probably be reduced to a single preg_replace with a negative lookahead, but I haven't quite mastered lookahead/lookbehinds yet, so maybe nrg will step in here and show me up or something. Quote Link to comment https://forums.phpfreaks.com/topic/162002-strip_tags-help/#findComment-854851 Share on other sites More sharing options...
nrg_alpha Posted June 13, 2009 Share Posted June 13, 2009 So yeah, that can probably be reduced to a single preg_replace with a negative lookahead, but I haven't quite mastered lookahead/lookbehinds yet, so maybe nrg will step in here and show me up or something. hehe.. I haven't mastered lookarounds either TBH. I don't make much use of them as most situations I run into don't require them. But I think I managed it here: Using your example string and going for the same end results you have, one possibility could be: $string = '<font>some text</font> more text here.. <font size="">something here</font> blah blah <font >more blah</font> mmm <font style="something">blah</font> more blah <font style = "" size="3"> blah </font>'; $string = preg_replace('#<font(??<!\bsize)[^>])*>(.+?)</font>#si', '$1', $string); Output: some text more text here.. <font size="">something here</font> blah blah more blah mmm blah more blah <font style = "" size="3"> blah </font> P.S Isn't <font> depreciated? People still use that? I hope the actual source code in question is from waaay back... becuase if it's recent... someone should learn css font manipulation instead. :/ Quote Link to comment https://forums.phpfreaks.com/topic/162002-strip_tags-help/#findComment-854984 Share on other sites More sharing options...
anarchoi Posted June 14, 2009 Author Share Posted June 14, 2009 thanks a lot yeah, the string i am doing replacements on are comming from veeeery old html pages Quote Link to comment https://forums.phpfreaks.com/topic/162002-strip_tags-help/#findComment-855537 Share on other sites More sharing options...
thebadbad Posted June 14, 2009 Share Posted June 14, 2009 thanks a lot yeah, the string i am doing replacements on are comming from veeeery old html pages Why not replace the deprecated tags with something newer then? Quote Link to comment https://forums.phpfreaks.com/topic/162002-strip_tags-help/#findComment-855651 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.