How Do You Comment Out the */ Part of a Regular Expression in PHP

安稳与你 提交于 2021-02-19 01:57:19

问题


I have preg_replace function that I'm calling and putting on multiple lines for readability but the */ characters in the regex mess up the comments. How can I comment out all these lines without moving them all onto one line?

return preg_replace('/.*/',
    'Lorem Ipsum' .
    'More Lorem Ipsum'
    ,
    $foo);

回答1:


You could use a different regex pattern delimiter character:

return preg_replace('#.*#',
    'Lorem Ipsum' .
    'More Lorem Ipsum'
    ,
    $foo);

EDIT: The delimiter character is a feature of PCRE (Perl Compatible Regular Expresssion). No PHP configuration is needed to use a different delimiter.

Regexp Quote-Like Operators

...you can use any pair of non-alphanumeric, non-whitespace characters as delimiters. This is particularly useful for matching path names that contain "/", to avoid LTS (leaning toothpick syndrome).

Quote and Quote-like Operators

Non-bracketing delimiters use the same character fore and aft, but the four sorts of ASCII brackets (round, angle, square, curly) all nest

These are all valid:

'/.*/'
'#.*#'
'{.*}' /* Note that '{.*{' would be incorrect. */

Take a look at PHP's documentation on PCRE Patterns to see a really good overview.



来源:https://stackoverflow.com/questions/12498284/how-do-you-comment-out-the-part-of-a-regular-expression-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!