Solved

Use regular expression to remove all <font> tags

Posted on 2008-10-03
10
461 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

706 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

18 Experts available now in Live!

Get 1:1 Help Now