2

我想用除 div 之外的特定字符串替换 som 字符。这是我的 str_replace :

// smileys
    $in = array(
        ':)',   
        ':D',
        ':o',
        ':p',
        ':(',
        ';)',
        'xD',
        '^^',
    );
    $out = array(
        '<img alt=":)" style="padding-left:3px;" src="img/emoticons/emoticon_smile.png" />',
        '<img alt=":D" style="padding-left:3px;" src="img/emoticons/emoticon_happy.png" />',
        '<img alt=":o" style="padding-left:3px;" src="img/emoticons/emoticon_surprised.png" />',
        '<img alt=":p" style="padding-left:3px;" src="img/emoticons/emoticon_tongue.png" />',
        '<img alt=":(" style="padding-left:3px;" src="img/emoticons/emoticon_unhappy.png" />',
        '<img alt=";)" style="padding-left:3px;" src="img/emoticons/emoticon_wink.png" />',
        '<img alt="xD" style="padding-left:3px;" src="img/emoticons/emoticon_evilgrin.png" />',
        '<img alt="^^" style="padding-left:3px;" src="img/emoticons/emoticon_happy.png" />'
    );
    $text = str_replace($in, $out, $text);

var $text 可以有<div class="code-geshi"></div>,但我不想让笑脸的 str_replace 进入。我该怎么做?

谢谢 :)

PS:对不起我的英语不好...

4

2 回答 2

0

你不能用 str_replace 做到这一点。使用preg_replace

于 2011-01-08T21:45:06.367 回答
0

我用了另一种方式。在我解析代码的函数中:

$text = preg_replace_callback('/\[code\="?(.*?)"?\](.*?)\[\/code\]/ms', "gen_geshi", $text);

我通过添加 chars 替换潜在的笑脸:

if (!function_exists('gen_geshi')) {
        function gen_geshi($s){
            global $text;
            $result = "";
            $list_languages = array('html4strict', 'php', 'javascript', 'css');
            $name_languages = array(
                'html4strict'   => 'HTML',
                'php'           => 'PHP',
                'javascript'    => 'Javascript',
                'css'           => 'CSS'
            );
            $text = strip_tags($text);
            $language = $s[1];
            $code = $s[2];

            $smileys_in = array(
                ':)',   
                ':D',
                ':o',
                ':p',
                ':(',
                ';)',
                'xD',
                '^^',
            );
            $smileys_out = array(
                '**-|-**:**-|-**)**-|-**',  
                '**-|-**:**-|-**D**-|-**',
                '**-|-**:**-|-**o**-|-**',
                '**-|-**:**-|-**p**-|-**',
                '**-|-**:**-|-**(**-|-**',
                '**-|-**;**-|-**)**-|-**',
                '**-|-**x**-|-**D**-|-**',
                '**-|-**^**-|-**^**-|-**',
            );

            $code = str_replace($smileys_in, $smileys_out, $code);

            if( in_array($language, $list_languages) && !empty($code) ){
                global $lang;
                $code = trim(preg_replace('#\t#', '  ', $code));
                if (!class_exists('GeSHi')) include('inc/geshi/geshi.php');
                $geshi = new GeSHi($code, $language);
                $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
                $result = '<div class="code-geshi-overall">' . $lang->get['global']['code'] . ' ' . $name_languages[$language] . ' : </div><div class="code-geshi">' . $geshi->parse_code() . '</div>';
            }

            return $result;
        }
    }

然后我使用了 str_replace :

$text = str_replace('**-|-**', '', $text);
于 2011-01-09T02:33:17.633 回答