如何删除字符串中的所有标点只是获取PHP中的空格分隔的单词

Suk*_*aul 5 php string

我想删除字符串中的任何类型的特殊字符,如下所示:

This is, ,,, *&% a ::; demo +  String.  +
Need to**@!/// format:::::
 !!! this.`
Run Code Online (Sandbox Code Playgroud)

需要输出:

This is a demo String Need to format this
Run Code Online (Sandbox Code Playgroud)

如何使用REGEX做到这一点?

Sam*_*son 15

检查非数字,非字母字符的任何重复实例,并用空格重复:

# string(41) "This is a demo String Need to format this"
$str = trim( preg_replace( "/[^0-9a-z]+/i", " ", $str ) );
Run Code Online (Sandbox Code Playgroud)

演示: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

  • 哇..谢谢你.它确实有效..还要感谢快速教程.这真的有帮助. (2认同)
  • 花时间来解释RegEx (2认同)