這是我目前的句子消毒功能:消毒句子PHP中的preg_replace
$string = ' Helloooooo my frieeend!!!What are you doing?? Tell me what you like...........,please. ';
結果是:
echo sanitize_sentence($string);
Helloooooo my frieeend! ! ! What are you doing? ? Tell me what you like. . . . . . . . . . . , please.
由於
# sanitize sentence
function sanitize_sentence($string) {
$string = preg_replace("/(?<!\d)[.,!?](?!\d)/", '$0 ', $string); # word,word. > word, word.
$string = preg_replace("/(^\s+)|(\s+$)/us", "", preg_replace('!\s+!', ' ', $string)); # " hello hello " > "hello hello"
return $string;
}
與此字符串運行一些測試你可以看到,我已經設法解決了一些要求,但我仍然堅持一些細節。最終的結果應該:
Helloo my frieend! What are you doing? Tell me what you like..., please.
這意味着,所有這些要求,應做到:
- 只能有一個或三個連續週期。或...
- 只能有一個連續的逗號,
- 只能有一個連續的問號?
- 可以只有一個連續的感嘆號!
- 一封信一個字不能重複超過2次。例如爲:質量(右),masss(錯了,應轉換爲質量)
- 的空間應該總是這些字符後加入,!?這已經很好了!
- 在連續3個週期的情況下,該空間僅在上一個週期後添加。
- 額外的空格(多於一個空格)應從句子的兩端刪除和修剪。 這已經很好了!
那麼你的問題是什麼? – 2014-11-14 15:36:10
所以你的結果與這些規則將如下:'Helloo我的朋友!你在做什麼?告訴我你喜歡什麼......請注意他和朋友...... – RichardBernards 2014-11-14 15:38:20
@RichardBernards是(固定最後一個字符串)。它不能防彈,所以最後的字符串會有地獄** oo **和星期五** end – andufo 2014-11-14 15:44:09