简体   繁体   English

PHP RegExp 某些字符

[英]PHP RegExp certain chars

What would be a good way to get rid of all chars except, letters, numbers, double quotes and hyphens.除了字母、数字、双引号和连字符之外,什么是摆脱所有字符的好方法。 This is what I got so far.这是我到目前为止得到的。

$test = preg_replace('#[^a-zA-Z0-9"-]#',' ',$string); 

Any suggestions?有什么建议么?

Thanks谢谢

Your regex is about as good a solution as you are going to find.您的正则表达式与您将要找到的解决方案一样好。

You can use \d to match digits, and the flag i to match az with case insensitivity.您可以使用 \d 来匹配数字,并使用标志 i 来匹配不区分大小写的 az。

$test = preg_replace('#[^a-z\d\w"-]#i','',$string);

Here is the php regex-syntax reference: http://se.php.net/manual/en/regexp.reference.php这是 php 正则表达式语法参考: http://se.php.net/manual/en/regexp.reference.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM