首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除php中的特殊字符

问删除php中的特殊字符
EN

Stack Overflow用户
提问于 2014-03-27 10:01:25
回答 4查看 10.7K关注 0票数 1

我有许多特殊字符的描述,如é,o等等,并试图用以下方式删除它们:

代码语言:javascript
复制
$string = str_replace("é", " ", $string)
$string = ereg_replace("é", " ", $string)
$string = mysql_real_escape_string($string)

但是没有工作,特殊字符仍然存在,并且描述没有插入到数据库中。我不能删除所有特殊字符,因为在描述中需要html标记。

谢谢你的帮助!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-03-27 10:13:12

轻松农民:

代码语言:javascript
复制
function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}

使用:

代码语言:javascript
复制
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!

代码语言:javascript
复制
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.
}

请参考此链接

票数 2
EN

Stack Overflow用户

发布于 2014-03-27 10:04:12

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

此函数返回带有替换值的字符串或数组。

您需要使用返回值。

代码语言:javascript
复制
$string = str_replace("é", " ", $string);
票数 2
EN

Stack Overflow用户

发布于 2014-03-27 10:04:30

使用字符音译。

示例:

代码语言:javascript
复制
<?php
$string = "ʿABBĀSĀBĀD";

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

?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22684097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档