How to remove non

I need to remove all characters from a string which aren't in az AZ 0-9 set or are not spaces.

Does anyone have a function to do this?


听起来你几乎已经知道你想要做什么,你基本上将它定义为一个正则表达式。

preg_replace("/[^A-Za-z0-9 ]/", '', $string);

对于unicode字符,它是:

preg_replace("/[^[:alnum:][:space:]]/u", '', $string);

Regular expression is your answer.

$str = preg_replace('/[^a-zd ]/i', '', $str);
  • The i stands for case insensitive.
  • ^ means, does not start with.
  • d matches any digit.
  • az matches all characters between a and z . Because of the i parameter you don't have to specify az and AZ .
  • After d there is a space, so spaces are allowed in this regex.
  • 链接地址: http://www.djcxy.com/p/58190.html

    上一篇: 如何获取PHP中的字符串的最后一个字符?

    下一篇: 如何删除非