1

Could someone tell me why this isn't working please?

$str = preg_replace("<font[^>]*>", '', $str);

CMS is for flash and now the client wants to implement a html website. Need to remove evil inline font tags to show default styling.

ComputerUser
  • 4,828
  • 15
  • 58
  • 71
  • Consider using a XML/SGML parser instead: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – You Aug 16 '10 at 07:40

4 Answers4

4

You can also try this one.

$str = preg_replace('/(<font[^>]*>)|(<\/font>)/', '', $str);
zriel
  • 79
  • 1
  • 9
0

if you want to use preg_replace have a look on this function (in this link you will found a lot of function to do this : http://php.net/manual/en/function.strip-tags.php)

Here is support for stripping content for the reverse strip_tags function:

<?php
function strip_only($str, $tags, $stripContent = false) {
    $content = '';
    if(!is_array($tags)) {
        $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
        if(end($tags) == '') array_pop($tags);
    }
    foreach($tags as $tag) {
        if ($stripContent)
             $content = '(.+</'.$tag.'[^>]*>|)';
         $str = preg_replace('#</?'.$tag.'[^>]*>'.$content.'#is', '', $str);
    }
    return $str;
}

$str = '<font color="red">red</font> text';
$tags = 'font';
$a = strip_only($str, $tags); // red text
$b = strip_only($str, $tags, true); // text
?> 
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
0

You don't have any delimiters on your pattern. This should work:

$str = preg_replace('/<font[^>]*>/', '', $str);

Obligatory "don't use regular expressions to parse HTML".

jasonbar
  • 13,333
  • 4
  • 38
  • 46
  • I am not really using regex to parse HTML. The Flash text editor throws out invalid garbage. Just need to remove the one tag with inline styles until I get someone in to create a vanilla text editor that can create both Flash + W3C valid HTML. – ComputerUser Aug 16 '10 at 07:47
0
#<font[^>]*>#

How isn't the delimiter there? The delimiter is #.

"A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character." php.net

Community
  • 1
  • 1
nush
  • 142
  • 2
  • 9