Solved

Use regular expression to remove all <font> tags

Posted on 2008-10-03
10
462 Views
Last Modified: 2012-05-05
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,
0
Comment
Question by:tiger0516
10 Comments
 
LVL 4

Expert Comment

by:imgriff
ID: 22637630
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
 
LVL 1

Author Comment

by:tiger0516
ID: 22637906
Thanks
But this is a White-List solution, I'd like a Black-List solution.
0
 
LVL 29

Expert Comment

by:fibo
ID: 22637970
<<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
 
LVL 18

Expert Comment

by:Hube02
ID: 22638043
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:tiger0516
ID: 22638057
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
 
LVL 1

Author Comment

by:tiger0516
ID: 22638076
Thanks Hube02

What about if users leave a space?

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

...
0
 
LVL 18

Expert Comment

by:Hube02
ID: 22638138
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
 
LVL 18

Accepted Solution

by:
Hube02 earned 500 total points
ID: 22638171
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
 
LVL 18

Expert Comment

by:Hube02
ID: 22638298
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now