我有许多特殊字符的描述,如é,o等等,并试图用以下方式删除它们:
$string = str_replace("é", " ", $string)
$string = ereg_replace("é", " ", $string)
$string = mysql_real_escape_string($string)但是没有工作,特殊字符仍然存在,并且描述没有插入到数据库中。我不能删除所有特殊字符,因为在描述中需要html标记。
谢谢你的帮助!
发布于 2014-03-27 10:13:12
轻松农民:
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}使用:
echo clean('a|"bc!@£de^&$f g');输出:abcdef-g
编辑
Hey, just a quick question, how can I prevent multiple hyphens from being next to each other? and have them replaced with just 1? Thanks in advance!
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}发布于 2014-03-27 10:04:12
http://www.php.net/manual/en/function.str-replace.php
此函数返回带有替换值的字符串或数组。
您需要使用返回值。
$string = str_replace("é", " ", $string);发布于 2014-03-27 10:04:30
使用字符音译。
示例:
<?php
$string = "ʿABBĀSĀBĀD";
echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $string);
// output: ABBASABAD
?>https://stackoverflow.com/questions/22684097
复制相似问题