Jump to content

strip_tags() help


anarchoi

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/162002-strip_tags-help/#findComment-854836
Share on other sites

$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.

Link to comment
https://forums.phpfreaks.com/topic/162002-strip_tags-help/#findComment-854851
Share on other sites

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. :/

Link to comment
https://forums.phpfreaks.com/topic/162002-strip_tags-help/#findComment-854984
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.