Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Use regular expression to remove all <font> tags

Posted on 2008-10-03
10
Medium Priority
?
478 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 dynamically set the form action using jQuery.

721 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