no str_replace en una etiqueta div

I want to replace som chars with a specific string except for a div. Here is my 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);

The var $text can have <div class="code-geshi"></div> but i ddon't want the str_replace for smileys go in. How can i do that?

Gracias :)

PS: Sorry for my bad english...

preguntado el 08 de enero de 11 a las 21:01

If $text contains markup you should use a Analizador DOM and only run your str_replace on the text nodes of the document to prevent accidental replacing in element attributes. -

2 Respuestas

You cannot do this with str_replace. Use preg_replace!

Respondido el 09 de enero de 11 a las 00:01

Ok, can you give me an example? Thx. - CrazyMax

Del manual: (?<!foo)bar does find an occurrence of "bar" that is not preceded by "foo". - rik

I used a different way. In my function that parse code :

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

I replace the potential smileys by adding 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;
        }
    }

And then i used a str_replace :

$text = str_replace('**-|-**', '', $text);

Respondido el 09 de enero de 11 a las 05:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.