删除php中的特殊字符

时间:2014-03-27 10:01:26

标签: php mysql special-characters

我有很多关于é,ô等特殊字符的描述,并试图将其删除:

$string = str_replace("é", " ", $string)
$string = ereg_replace("é", " ", $string)
$string = mysql_real_escape_string($string)

但没有任何作用,特殊字符仍然存在,并且描述未插入数据库中。我无法删除所有特殊字符,因为在描述中有所需的html标记。

感谢您的帮助!

4 个答案:

答案 0 :(得分:2)

http://www.php.net/manual/en/function.str-replace.php

  

此函数返回包含替换值的字符串或数组。

您需要使用返回值

$string = str_replace("é", " ", $string);

答案 1 :(得分:1)

轻松自负:

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.
}

refer this link

答案 2 :(得分:0)

使用character transliteration

示例:

<?php
$string = "ʿABBĀSĀBĀD";

echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $string);
// output: ABBASABAD

?>

答案 3 :(得分:0)

mysql_real_escape_string(htmlentities($string));