BFTrick
BFTrick

Reputation: 5441

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

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);

Upvotes: 5

Views: 202

Answers (1)

jimp
jimp

Reputation: 17477

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.

Upvotes: 9

Related Questions