[英]Beginner PHP help needed
I have been learning PHP for some time now and I wanted one clarification. 我已经学习PHP已有一段时间了,我想澄清一下。
I have seen the preg_match
function called with different delimiting symbols like: 我看过
preg_match
函数调用时使用了不同的定界符号,例如:
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 任何
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 : 为了避免转义,最好选择其他定界符,它不在regex中,这样您的regex会更短更整洁 :
if(preg_match(#^\d+/\d+$#))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.