2014-11-14 71 views
0

這是我目前的句子消毒功能:消毒句子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. 

這意味着,所有這些要求,應做到:

  1. 只能有一個或三個連續週期...
  2. 只能有一個連續的逗號
  3. 只能有一個連續的問號
  4. 可以只有一個連續的感嘆號
  5. 一封信一個字不能重複超過2次。例如爲:質量(右),masss(錯了,應轉換爲質量
  6. 的空間應該總是這些字符後加入,!?這已經很好了!
  7. 在連續3個週期的情況下,該空間僅在上一個週期後添加。
  8. 額外的空格(多於一個空格)應從句子的兩端刪除和修剪。 這已經很好了!
+0

那麼你的問題是什麼? – 2014-11-14 15:36:10

+1

所以你的結果與這些規則將如下:'Helloo我的朋友!你在做什麼?告訴我你喜歡什麼......請注意他和朋友...... – RichardBernards 2014-11-14 15:38:20

+0

@RichardBernards是(固定最後一個字符串)。它不能防彈,所以最後的字符串會有地獄** oo **和星期五** end – andufo 2014-11-14 15:44:09

回答

1

我覺得正則表達式是這是一個非常合適的技術。畢竟,這完全是消極的。不是語法或語法更正。

function sanitize_sentence($i) { 

    $o = $i; 

    // There can be only one or three consecutive periods . or ... 
    $o = preg_replace('/\.{4,}/','… ',$o); 
    $o = preg_replace('/\.{2}/','. ',$o); 

    // There can be only one consecutive "," 
    $o = preg_replace('/,+/',', ',$o); 

    // There can be only one consecutive "!" 
    $o = preg_replace('/\!+/','! ',$o); 

    // There can be only one consecutive "?" 
    $o = preg_replace('/\?+/','? ',$o); 

    // we just preemptively added a bunch of spaces. 
    // Let's remove any spaces between punctuation marks we may have added 
    $o = preg_replace('/([^\s\w])\s+([^\s\w])/', '$1$2', $o); 

    // A letter cannot repeat itself more than 2 times in a word 
    $o = preg_replace('/(\w)\1{2,}/','$1$1',$o); 

    // Extra spaces should be eliminated 
    $o = preg_replace('/\s+/', ' ', $o); 
    $o = trim($o); 

    // we want three literal periods, not an ellipsis char 
    $o = str_replace('…','...',$o); 

    return $o; 
} 
+0

您可能想檢查',!?'在搜索中它們之後沒有空格,所以用它替換它:preg_replace('/,+ * /',',',$ o) – OnlineCop 2014-11-14 16:31:19

+0

請注意,該腳本會將3個或更多個句點轉換爲[省略號字符](http://www.fileformat.info/info/unicode/char/2026/index.htm),而不是3個時期。如果您希望將期限保留爲期限 – 2014-11-14 16:35:44

+0

@OnlineCop,以便適應OP的第6條要求,則需要稍微重構。我的代碼包含一個條款,試圖糾正標點符號之間的任何不需要的空格 – 2014-11-14 16:38:50

1

我想我會一次回答一個問題,因爲一次只專注於一個任務而不是將它們全部集中在一起更有意義。

對於#5,我建議([a-z])(\1{0,1})\1*$1$2代替,如看到in this example

它需要輸入

 Helloooooo my frieeend!!!What are you doing?? Tell me what you like...........,please. 

併產生輸出

 Helloo my frieend!!!What are you doing?? Tell me what you like...........,please. 
+0

有趣的是,它可以在regex101網站上運行,但不能在我的apache服務器上使用php 5.4.4 - 由於某些原因,它不能識別** g **修飾符,這在http://php.net中也不可用/manual/es/reference.pcre.pattern.modifiers.php – andufo 2014-11-14 16:00:52

+1

'g'修飾符只會向preg_replace添加一個額外的字段:從'preg_replace(...)'到'preg_replace(...,1)'。在regex101網站上,點擊左側的「代碼生成器」以查看代碼差異。 – OnlineCop 2014-11-14 16:13:43

0

對於#1(...),(?<!\.)(\.{3}|\.)\.*\s*可以與$1更換(注意結尾空間),如this example所示。

這需要

 Helloooooo my frieeend!!!What are you doing?? Tell me what you like...........,please. 

,併產生輸出

 Helloooooo my frieeend!!!What are you doing?? Tell me what you like... ,please. 

正如你所看到的,你會得到一個時髦的... ,性格,這是一個更可能需要檢查的事情。除非您有另一個規則可用於刪除多個標點符號,否則您可以在執行此清理之前檢查.,或之後檢查. ,(之間的空格)。

這個生成的代碼,從regex101.com網站,如下:

$re = "/(?<!\\.)(\\.{3}|\\.)\\.*\\s*/"; 
$str = "  Helloooooo my frieeend!!!What are you doing?? Tell me what you like...........,please. "; 
$subst = "$1 "; 
$result = preg_replace($re, $subst, $str); 
0

#2,#3,#4,你可以搜索([,?!])\1+\s*$1替換(請注意之後的空間)如在this example中那樣。

這需要

 Helloooooo my frieeend!!!What are you doing?? Tell me what you like...........,please. 

,併產生

 Helloooooo my frieeend! What are you doing? Tell me what you like...........,please. 

生成的代碼看起來像:

$re = "/([,?!])\\1+\\s*/"; 
$str = "  Helloooooo my frieeend!!!What are you doing?? Tell me what you like...........,please. "; 
$subst = "$1 "; 
$result = preg_replace($re, $subst, $str);