需要新手PHP帮助 [英] Beginner PHP help needed
问题描述
我已经学习PHP已有一段时间了,我想澄清一下。
I have been learning PHP for some time now and I wanted one clarification.
我见过 preg_match
用不同的定界符号调用的函数,例如:
I have seen the preg_match
function called with different delimiting symbols like:
preg_match('/.../')
和
preg_match('#...#')
今天我还看到了%
被使用。
Today I also saw %
being used.
我的问题分为两个部分:
My question is two part:
可以使用所有字符吗?
有标准吗?
推荐答案
任何
- 非字母数字
- 非空白和
- 非反斜杠ASCII字符
可以用作分隔符。
另外,如果您使用开头标点符号作为开头分隔符:
Also if you using the opening punctuation symbols as opening delimiter:
( { [ <
然后必须使用其对应的 closing 标点符号作为结束分隔符:
then their corresponding closing punctuation symbols must be used as closing delimiter:
) } ] >
最常见的分隔符是 /
。< br>但有时,如果 /
是正则表达式的一部分,则建议使用其他定界符。
The most common delimiter is /
.
But sometimes it's advised to use a different delimiter if a /
is part of the regex.
例如:
// 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+$#))
这篇关于需要新手PHP帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!