【发布时间】:2014-11-28 21:49:51
【问题描述】:
我正在尝试通过仅过滤掉符合以下条件的单词来匹配字符串中的关键字:
- 包含大写字母的单词,例如“iPhone”或“camelCase”
- 连续大写单词组,例如“Pittsburgh Steelers”或“Oscar De La Hoya”
- 结合上述标准,如“iPhone 5”或“MIB 2”(也将数字视为大写)
- 折叠所有非字符/数字,因此“O'Donnell's”将变为“ODonnells”,“Wi-fi...”将变为“Wifi”
例子:
$string = "Joe O'Donnell and Oscar De La Hoya went to a Pittsburgh Steelers game on Sunday, where Joe lost his iPhone 5, so he borrowed Oscar's iPad";
preg_match_all("/[A-Z][a-z]*/",$string,$match_words); // incorrect expression
// desired result for $match_words should be:
// array(Joe ODonnell, Oscar De La Hoya, Pittsburgh Steelers, Sunday, Joe, iPhone 5, Oscars, iPad)
谢谢
【问题讨论】:
-
必须是 preg_match 吗?是否也可以接受不同的解决方案?
-
@BrianGlaz 当然。什么都行。显然效率越高越好。
-
@John 高效且防弹:)
标签: php regex preg-match-all