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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.