开发者

Beginner PHP help needed

I have been learning PHP for some time now and I wanted one clarification.

I have seen the preg_match function called with different delimiting symbols like:

开发者_如何学Python
preg_match('/.../')

and

preg_match('#...#')

Today I also saw % being used.

My question is two part:

What all characters can be used?

And is there a standard ?


Any

  • non-alphanumeric
  • non-whitespace and
  • non-backslash ASCII character

can be used as delimiter.

Also if you using the opening punctuation symbols as opening delimiter:

( { [ <

then their corresponding closing punctuation symbols must be used as closing delimiter:

) } ] >

The most common delimiter is /.
But sometimes it's advised to use a different delimiter if a / is part of the regex.

Example:

// check if a string is number/number format:
if(preg_match(/^\d+\/\d+$/)) {
  // match
}

Since the regex contains the delimiter, you must escape the delimiter found in the regex.

To avoid the escaping it is better to choose a different delimiter, one which is not present in the regex, that way your regex will be shorter and cleaner:

if(preg_match(#^\d+/\d+$#)) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜