2016-01-03 11 views
7

sto cercando di aggiungere commenti a fare un'espressione regolare più chiaramodo corretto di aggiungere commenti a un'espressione regolare in 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); 

ma ho l'avvertimento

Attenzione: preg_replace(): Modificatore sconosciuto "/" nella riga 280

Come devo commentare?

+0

Probabilmente dovresti separare ciascun pezzo e concatenarli con l'operatore '.'. – segFault

+0

Non si possono fare commenti all'interno di una classe di caratteri. gli spazi bianchi sono sempre significativi all'interno di una classe di caratteri, anche con il modificatore x. –

risposta

5

Questo potrebbe non essere l'approccio più pulito, ma è possibile racchiudere ciascuna sezione tra virgolette e concatenarle.

Qualcosa del genere dovrebbe funzionare:

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

In alternativa ho trovato this nei documenti PHP.

Quindi immagino che potrebbe funzionare anche, ma si sta utilizzando il modificatore x e che dovrebbe funzionare già.

Se è impostata l'opzione PCRE_EXTENDED, un carattere # senza escape di fuori di una classe di caratteri introduce un commento che continua fino al successivo carattere di fine riga nel modello.

Ciò indica che tutti i commenti all'interno di un set di caratteri [...] non sono validi.

Ecco un esempio di lavoro per l'utilizzo con il modificatore 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

Come indicato in altri punti, è necessario rimuovere o eliminare i delimitatori nel commento –

+3

È preferibile modificare il delimitatore in '~' –

4

Questo è stato portato in a comment on the php.net modifiers page.

Per citare:

Quando si aggiungono i commenti con il/x modificatore, non utilizzare i delimitatori nei commenti. Potrebbe non essere ignorato nell'area dei commenti.

Nel tuo esempio, uno dei tuoi commenti ha la stringa :// incorporata al suo interno. Dal momento che PHP sembra non analizzare i delimitatori di espressioni regolari prendendo in considerazione i flag, vede questo come un problema. Lo stesso può essere visto con il codice qui sotto:

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

Demo

si avrebbe bisogno di cambiare il vostro sia delimitatore o la fuga il delimitatore nei commenti.

Problemi correlati