Solved

Use regular expression to remove all <font> tags

Posted on 2008-10-03
10
467 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 look for a specific file type in a local or remote server directory using PHP.

786 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