tags:

views:

38

answers:

3

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.

A: 

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
Thanks, answer then is. $str = preg_replace("#<font[^>]*>#is", '', $str);
JasonS
yes preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
Haim Evgi
A: 

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
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.
JasonS
A: 
#<font[^>]*>#

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

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

nush