2 回答

TA貢獻1831條經驗 獲得超9個贊
我已經在這方面工作了一段時間。評論和 Taha Paksu 非常有效的解決方案有助于幫助我思考問題。除了重音字母之外,Taha Paksu 的解決方案完全隔離了單詞。谷歌搜索似乎表明 RegEx 對非 ascii 字符不太友好。
正是當我放棄嘗試做正則表達式伏都教(任何可以得到我最深切尊重的人)時,我才想到了這個不太優雅的 hack。
$text = "Testing text. Café is spelled true. And pokémon too... ‘bad quotes’. (brackets)... L?wen, B?ren, V?gel und K?fer sind Tiere. That’s what I said.";
$text = str_replace(array('’',"'"), '000AP000', $text);
$text = str_replace("-", '000HY000', $text);
$text = preg_replace("/[^[:alnum:][:space:]]/u", ' ', $text);
$text = str_replace('000AP000', "'", $text);
$text = str_replace('000HY000', "-", $text);
$text = str_replace(array("' ",'- ',' '," '",' -',' '), ' ', $text);
$words = mb_split( ' +', $text );
它使用兩個統計上不太可能的字符串作為占位符,清理其余的字符串,將連字符和撇號放回原處,然后取出任何接觸空格(和多個空格)的東西。它適用于我能找到的所有東西。
如果可以,我想找到一個不那么繁瑣的解決方案,但我的正則表達式技能可能無法勝任這項任務(即使打開了備忘單)。
- 2 回答
- 0 關注
- 194 瀏覽
添加回答
舉報