开发者

how to strip all characters except for alphanumeric and underscore and dash?

I'm not an expert with regex:( I'm trying to to 开发者_JS百科strip all characters from the string except for alpanumeric and underscore and dash. Is this the correct syntax?:

preg_replace("/[^a-z0-9_-]+/i", "", $string);


Yes, but it can be optimised slightly:

preg_replace('/[^\w-]/', '', $string);

\w matches alphanumeric characters and underscores. This has the added advantage of allowing accented characters if your locale allows.


What you have looks like it will work. You may want to add spaces since they're not an alphanumeric character:

preg_replace("/[^a-z0-9_-\s]+/i", "", $string);


Yes. :)

http://codepad.org/lkJTRP0P

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜