Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Use regular expression to remove all <font> tags

Posted on 2008-10-03
10
Medium Priority
?
479 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

963 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