我正在尝试添加注释以使正则表达式更清晰
// 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行的未知修饰符'/'
我该如何评论呢?
这可能不是最干净的方法,但您可以将每个部分括在引号中并将它们连接起来.
这样的事情应该有效:
$pattern = '('. // First capturing group '(http|https)'. // Second capturing grout,matches wither http or https '\:\/\/)?'. // End of first capturing group, matches :// exactly ...
或者我在PHP文档中找到了这个.
所以我想这也会起作用,但你正在使用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 ';