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,
LVL 1
tiger0516Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Hube02Connect With a Mentor Commented:
A space is not handled, sorry, missed the last post, to includ optional spaces at the beginning you can add the optional space:

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

That will allow for any number of white space characters as well as the optional ?
0
 
imgriffCommented:
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>");

0
 
tiger0516Author Commented:
Thanks
But this is a White-List solution, I'd like a Black-List solution.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Bernard S.CTOCommented:
<<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?
0
 
Hube02Commented:
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

0
 
tiger0516Author Commented:
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.

0
 
tiger0516Author Commented:
Thanks Hube02

What about if users leave a space?

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

...
0
 
Hube02Commented:
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);

 
0
 
Hube02Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.