在php中,应该经常用到str_replace()字符替代函数吧。但是你有没有想过str_replace(search, replace, $str); 前面两个参数可以是数组吗?
$text = ":D";
$text_smiles = array(":)", ":D", ":(", ":'(", ":@", ":P" );
$image_smiles = array("<img src='smile.gif'>", "<img src='laugh.gif'>",
"<img src='sad.gif'>", "<img src='cry.gif'>",
"<img src='mad.gif'>", "<img src='tounge.gif'>");
// Replace input with smileys if present
$content = str_replace($text_smiles, $image_smiles, $text);
echo $content;
数组替代,这种作用可就大了。
<?php
$bad_words = ('duck', 'goose', 'beach', 'mother trucker', 'and many more');
$replace = '***';
$text = $_POST['input'];
$filtered = str_replace($bad_words, $replace, $text);
?>
屏蔽码也可以用它。真是太强大了。