php - 如何删除字符串中的所有标点符号,只需在 PHP 中获取由空格分隔的单词

标签 php string

我想像这样删除字符串中的任何类型的特殊字符:

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/

相关文章:

php - 在 mysql 表中存储数据的更好方法是什么?

c++ - 试图附加一个 char 字符串

Javascript + 从变量名和字符串构建动态变量名

c++ - C++Builder 6 上的 std::string 操作错误

php - 为什么我的准备语句不起作用?

php - mysql php - 数据没有完全进入数据库

ios - 如何为 UITableView 中的最后一个 UITableViewCell 创建 XCUIElement 查询?

c# - 在字符串 : "Input string was not in a correct format" 中使用括号时

php - 如何根据比较将整个结果查询分类?

php - 如何在微软的php graph api中访问响应数据