Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 258
BFTrick Avatar asked Sep 19 '12 15:09

BFTrick


1 Answers

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.

like image 60
jimp Avatar answered Nov 14 '22 15:11

jimp