in php how to remove all special characters, upper

2019-05-29 20:14发布

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"]));

2条回答
干净又极端
2楼-- · 2019-05-29 20:54

I'm guessing you are trying to remove everything except lowercase letters. If that is the case, try this:

preg_replace('/[^a-z]/', "", strtolower($_GET["myvar"]));

This is will transform $_GET["myvar"] to all lowercase letters then remove anything that isn't a lowercase letter.

查看更多
孤傲高冷的网名
3楼-- · 2019-05-29 20:56
preg_replace('/[^a-z]/', '', strtolower($_GET['myvar']));

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 (//).

查看更多
登录 后发表回答