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?
Thanks :)
PS: Sorry for my bad english...
You cannot do this with str_replace. Use preg_replace!
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);
精彩评论