开发者

PHP - Regex, remove font tag

开发者 https://www.devze.com 2023-01-12 04:58 出处:网络
Could someone tell me why this isn\'t working please? $str = preg_replace(\"<font[^>]*>\", \'\', $str);

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

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

CMS is for flash and now the client wants 开发者_如何学JAVAto implement a html website. Need to remove evil inline font tags to show default styling.


You can also try this one.

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


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


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


#<font[^>]*>#

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消