Link to home
Start Free TrialLog in
Avatar of tiger0516
tiger0516

asked on

Use regular expression to remove all <font> tags

I have lots of pages and there are tons of <font size=... ... color =... ...> ... </font> tags. I want to remove all of them. Just keep the text between <font> and </font>

How do I use preg_replace to move all those tags?

Thanks,
Avatar of imgriff
imgriff

An alternative could be PHP's function strip_tags()

http://us.php.net/strip_tags

$string = strip_tags($string, "<p><a><img><script><div><span>");

Avatar of tiger0516

ASKER

Thanks
But this is a White-List solution, I'd like a Black-List solution.
Avatar of Bernard Savonet
<<But this is a White-List solution, I'd like a Black-List solution.>>
??
Can you elaborate and give more details ob what you want to achieve?
To simply remove all instances of <font...> or </font>
<?php
  
  $string = '<font color="red"><font><font size="2">text</font> more text</font> and more text</font>';
  
  while(preg_match('/<\/?font[^>]*>/i', $string, $matches)) {
    $string = str_replace($matches[0], '', $string);
  }
  
  echo $string;
  
?>

Open in new window

ok.

php's strip_tags gives a list of ALLOWED tags and remove all others
I want to give a list of NOT ALLOWED tags and keep all others.

Thanks Hube02

What about if users leave a space?

eg:
< font >
< font>
< /font>

...
Don't know what I was thinking with that while loop, the loop is not needed. preg_replace will replace all occurrences by default, so all you need is:

$string = preg_replace('/<\/?font[^>]*>/', '', $string);

 
ASKER CERTIFIED SOLUTION
Avatar of Hube02
Hube02
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I should mention however that that will also replace something like

< font tags are not wanted >

However, I find it hard to think of why someone would use angel brackets and the word font or fonts in a sentence.