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答案.
试图取代正常的期望
更改 preg_replace('/[^A-Za-z0-9-]/',$string); 同 preg_replace('/[^A-Za-z0-9-']/',$string); // escape apostraphe 要么 你可以str_replace它比preg_replace()更快更容易因为它不使用正则表达式. $text = str_replace("'",$string); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |