我想像这样删除字符串中的任何类型的特殊字符:
This is, ,,, *&% a ::; demo + String. +
Need to**@!/// format:::::
!!! this.`
需要的输出:
This is a demo String Need to format this
如何使用 REGEX 执行此操作?
最佳答案
检查非数字、非字母字符的任何重复实例并用空格重复:
# string(41) "This is a demo String Need to format this"
$str = trim( preg_replace( "/[^0-9a-z]+/i", " ", $str ) );
演示:http://codepad.org/hXu6skTc
/ # Denotes start of pattern [ # Denotes start of character class ^ # Not, or negative 0-9 # Numbers 0 through 9 (Or, "Not a number" because of ^ a-z # Letters a through z (Or, "Not a letter or number" because of ^0-9 ] # Denotes end of character class + # Matches 1 or more instances of the character class match / # Denotes end of pattern i # Case-insensitive, a-z also means A-Z
关于php - 如何删除字符串中的所有标点符号,只需在 PHP 中获取由空格分隔的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10810553/