在php中如何删除所有特殊字符,大写字母,数字和空格
这是我到目前为止所得到的,但我似乎无法找出删除空格的正确方法。有什么想法吗?
preg_replace('[a-z]', "", strtolower($_GET["myvar"]));
This is what i've got so far, but i cant seem to figure out the proper way to have it remove spaces. Any ideas?
preg_replace('[a-z]', "", strtolower($_GET["myvar"]));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜你正在尝试删除除小写字母之外的所有内容。如果是这种情况,请尝试以下操作:
这会将
$_GET["myvar"]
转换为所有小写字母,然后删除所有非小写字母。I'm guessing you are trying to remove everything except lowercase letters. If that is the case, try this:
This is will transform
$_GET["myvar"]
to all lowercase letters then remove anything that isn't a lowercase letter.从技术上讲,不可能有任何大写字母,因为您保证在正则表达式获取字符串之前所有字母都将是小写的。无论如何,此正则表达式将删除任何不属于 az 的内容。
您几乎已经完成了,只是缺少反转 (
^
) 和分隔符 (//
)。technically, there couldn't ever be any upper case letters, since you're guaranteeing that all letters will be lower case before the regex ever gets its hands on the string. In any case, this regex will remove anything that ISN'T a-z.
You almost had it, and were just missing the inversion (
^
) and the delimiters (//
).