问题
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