加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 从字符串中删除特殊字符

发布时间:2020-12-13 16:23:57 所属栏目:PHP教程 来源:网络整理
导读:我正在使用一个功能从字符串中删除特殊字符. function clean($string) { $string = str_replace('','-',$string); // Replaces all spaces with hyphens. return preg_replace('/[^A-Za-z0-9-]/','',$string); // Removes special chars.} 这里是测试用例 e
我正在使用一个功能从字符串中删除特殊字符.
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');
Will output: abcdef-g

参考从SO答案.
问题是如果’是我的字符串中的最后一个字符,就像我从一个excel文件中得到一个字符串America’,如果我把这个函数放在这个函数中,它不会逃脱’.当第一个和最后一个字符是’

试图取代正常的期望
更改
preg_replace('/[^A-Za-z0-9-]/',$string);

preg_replace('/[^A-Za-z0-9-']/',$string);  // escape apostraphe

要么

你可以str_replace它比preg_replace()更快更容易因为它不使用正则表达式.

$text = str_replace("'",$string);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读