2016-01-03 23 views
7
添加到正則表達式正確方法

我想添加註釋,使一個正則表達式更清晰到的意見在PHP

// Strip any URLs (such as embeds) taken from http://stackoverflow.com/questions/6427530/regular-expression-pattern-to-match-url-with-or-without-http-www 
$pattern = 
    '(        # First capturing group 
      (http|https)    # Second capturing grout,matches wither http or https 
     \:\/\/)?      # End of first capturing group, matches :// exactly 
     [       # Match any char in the following list. the + after the closing bracke means greedy 
      a-z      # Any char between a and z 
      A-Z      # Any char between A and Z 
      0-9      # Any char between 0 and 9 
      \.\/\?\:@\-    # ./?:@- literally (any one of them) 
      _=#      # _=# any of these thre chars 
     ]+       # end of list 
     \.       # matches . 
     (       # third caturing group 
      [      # start of list 
       a-z     # Any char between a and z 
       A-Z     # Any char between A and Z 
       0-9     # Any char between 0 and 9 
       \.\/\?\:@\-   # ./?:@- literally (any one of them) 
       _=#     # _=# any of these thre chars 
      ]      # end of list 
     )*       # end of capturing group with greedy modifier'; 
$excerpt = preg_replace("/$pattern/x", '', $excerpt); 

,但我得到的警告

警告:的preg_replace():在上線280

我應該如何評論這未知的修飾詞「/」?

+0

你可能不得不分開每一塊,並用'.'運算符連接它們。 – segFault

+0

你只能在角色類中留言。即使使用x修飾符,空白在字符類中始終是重要的。 –

回答

5

這可能不是最乾淨的方法,但可以將每個部分用引號括起來並連接它們。

像這樣的東西應該工作:

$pattern = 
    '('.        // First capturing group 
     '(http|https)'.    // Second capturing grout,matches wither http or https 
    '\:\/\/)?'.      // End of first capturing group, matches :// exactly 
    ... 

或者我在PHP文檔中發現this

所以我想這也可以,但你使用的是x修飾符,這應該已經工作。

如果設置了PCRE_EXTENDED選項,字符類外部的未轉義的#字符將引入一個註釋,該註釋會繼續到該模式中的下一個換行符。

這表示字符集[...]內的所有評論無效。

這裏是與PCRE_EXTENDED改性劑使用工作的例子:

$pattern = ' 
    (        # First capturing group 
     (http[s]?)     # Second capturing grout,matches wither http or https 
    \:\/\/)?      # End of first capturing group, matches :// exactly 
    [a-zA-Z0-9\.\/\?\:@\-_=#]+  # [List Comment Here] 
    \.        # matches . 
    (        # third caturing group 
     [a-zA-Z0-9\.\/\?\:@\-_=#] # [List Comment Here] 
    )*        # end of capturing group with greedy modifier 
'; 
+1

正如其他人指出,你需要刪除或轉義分隔符 –

+3

最好將分隔符更改爲'〜' –

4

這被帶到了in a comment on the php.net modifiers page

報價:

當與/ X修改添加註釋,不要在評論中使用該模式分隔符。它在評論區可能不會被忽略。

在您的示例中,其中一條評論的字符串嵌入了://。由於PHP似乎不考慮標記來解析正則表達式分隔符,它將這看作是一個問題。同樣可以看到與下面的代碼:

echo preg_replace('/ 
a #Com/ment 
/x', 'e', 'and'); 

Demo

你會需要更改您的分隔符或逃避評論的分隔符。