I have this code :
我有这个代码:
$string1 = "My name is 'Kate' and im fine"; $pattern = "My name is '(.*)' and im fine"; preg_match($pattern , $string1, $matches);echo $matches[1];
and when im run it returns this error:
当我运行它时会返回此错误:
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash
警告:preg_match()[function.preg-match]:分隔符不能是字母数字或反斜杠
5 个解决方案
#1
96
You need a delimiter for your pattern. It should be added at the start and end of the pattern like so:
您需要为模式定界。应该在模式的开头和结尾添加它,如下所示:
$pattern = "/My name is '(.*)' and im fine/"; // With / as a delimeter
#2
41
The solution (which other answers don't mention—at least at the time of my originally writing this) is that when PHP refers to delimiters, it's not referring to the delimiters you see in your code (which are quote marks) but the next characters inside the string. (In fact I've never seen this stated anywhere in any documentation: you have to see it in examples.) So instead of having a regular expression syntax like what you may be accustomed to from many other languages:
解决方案(其他答案没有提及 - 至少在我最初写这篇文章的时候)是当PHP引用分隔符时,它不是指你在代码中看到的分隔符(引号),而是下一个分隔符。字符串中的字符。 (事实上,我从未在任何文档中看到过这种说法:你必须在示例中看到它。)因此,不要使用正常的表达式语法,就像你可能习惯于许多其他语言一样:
/something/
PHP uses strings, and then looks inside the string for another delimiter:
PHP使用字符串,然后在字符串中查找另一个分隔符:
'/something/'
The delimiter PHP is referring to is the pair of /
characters, instead of the pair of '
characters. So if you write 'something'
, PHP will take s
as the intended delimiter and complain that you're not allowed to use alphanumeric characters as your delimiter.
PHP引用的分隔符是一对/字符,而不是一对'字符。因此,如果你写'某事',PHP将把s作为预定的分隔符,并抱怨你不允许使用字母数字字符作为分隔符。
So if you want to pass (for instance) an i
to show that you want a case-insensitve match, you pass it inside the string but outside of the regex delimiters:
因此,如果你想传递(例如)一个i来表明你想要一个不区分大小写的匹配,你将它传递给字符串但在正则表达式分隔符之外:
'/something/i'
If you want to use something other than /
as your delimiter, you can, such as if you're matching a URL and don't want to have to escape all the slashes:
如果你想使用/以外的其他东西作为分隔符,你可以,例如,如果你匹配一个URL,并且不想要逃避所有斜杠:
'~something~'
#3
18
You must specify a delimiter for your expression. A delimiter is a special character used at the start and end of your expression to denote which part is the expression. This allows you to use modifiers and the interpreter to know which is an expression and which are modifiers. As the error message states, the delimiter cannot be a backslash because the backslash is the escape character.
您必须为表达式指定分隔符。分隔符是在表达式的开头和结尾使用的特殊字符,用于表示哪个部分是表达式。这允许您使用修饰符和解释器来知道哪个是表达式,哪个是修饰符。正如错误消息所述,分隔符不能是反斜杠,因为反斜杠是转义字符。
$pattern = "/My name is '(.*)' and im fine/";
and below the same example but with the i
modifier to match without being case sensitive.
并且在同一个示例下面,但是i修饰符匹配而不区分大小写。
$pattern = "/My name is '(.*)' and im fine/i";
As you can see, the i
is outside of the slashes and therefore is interpreted as a modifier.
如您所见,i在斜杠之外,因此被解释为修饰符。
Also bear in mind that if you use a forward slash character (/) as a delimiter you must then escape further uses of / in the regular expression, if present.
另外请记住,如果使用正斜杠字符(/)作为分隔符,则必须在正则表达式中进一步使用/(如果存在)。
#4
5
The pattern must have delimiters. Delimiters can be a forward slash (/) or any non alphanumeric characters(#,$,*,...). Examples
该模式必须具有分隔符。分隔符可以是正斜杠(/)或任何非字母数字字符(#,$,*,...)。例子
$pattern = "/My name is '(.*)' and im fine/"; $pattern = "#My name is '(.*)' and im fine#";$pattern = "@My name is '(.*)' and im fine@";
#5
1
Please try with this
请尝试这个
$pattern = "/My name is '\(.*\)' and im fine/";
#1
96
You need a delimiter for your pattern. It should be added at the start and end of the pattern like so:
您需要为模式定界。应该在模式的开头和结尾添加它,如下所示:
$pattern = "/My name is '(.*)' and im fine/"; // With / as a delimeter
#2
41
The solution (which other answers don't mention—at least at the time of my originally writing this) is that when PHP refers to delimiters, it's not referring to the delimiters you see in your code (which are quote marks) but the next characters inside the string. (In fact I've never seen this stated anywhere in any documentation: you have to see it in examples.) So instead of having a regular expression syntax like what you may be accustomed to from many other languages:
解决方案(其他答案没有提及 - 至少在我最初写这篇文章的时候)是当PHP引用分隔符时,它不是指你在代码中看到的分隔符(引号),而是下一个分隔符。字符串中的字符。 (事实上,我从未在任何文档中看到过这种说法:你必须在示例中看到它。)因此,不要使用正常的表达式语法,就像你可能习惯于许多其他语言一样:
/something/
PHP uses strings, and then looks inside the string for another delimiter:
PHP使用字符串,然后在字符串中查找另一个分隔符:
'/something/'
The delimiter PHP is referring to is the pair of /
characters, instead of the pair of '
characters. So if you write 'something'
, PHP will take s
as the intended delimiter and complain that you're not allowed to use alphanumeric characters as your delimiter.
PHP引用的分隔符是一对/字符,而不是一对'字符。因此,如果你写'某事',PHP将把s作为预定的分隔符,并抱怨你不允许使用字母数字字符作为分隔符。
So if you want to pass (for instance) an i
to show that you want a case-insensitve match, you pass it inside the string but outside of the regex delimiters:
因此,如果你想传递(例如)一个i来表明你想要一个不区分大小写的匹配,你将它传递给字符串但在正则表达式分隔符之外:
'/something/i'
If you want to use something other than /
as your delimiter, you can, such as if you're matching a URL and don't want to have to escape all the slashes:
如果你想使用/以外的其他东西作为分隔符,你可以,例如,如果你匹配一个URL,并且不想要逃避所有斜杠:
'~something~'
#3
18
You must specify a delimiter for your expression. A delimiter is a special character used at the start and end of your expression to denote which part is the expression. This allows you to use modifiers and the interpreter to know which is an expression and which are modifiers. As the error message states, the delimiter cannot be a backslash because the backslash is the escape character.
您必须为表达式指定分隔符。分隔符是在表达式的开头和结尾使用的特殊字符,用于表示哪个部分是表达式。这允许您使用修饰符和解释器来知道哪个是表达式,哪个是修饰符。正如错误消息所述,分隔符不能是反斜杠,因为反斜杠是转义字符。
$pattern = "/My name is '(.*)' and im fine/";
and below the same example but with the i
modifier to match without being case sensitive.
并且在同一个示例下面,但是i修饰符匹配而不区分大小写。
$pattern = "/My name is '(.*)' and im fine/i";
As you can see, the i
is outside of the slashes and therefore is interpreted as a modifier.
如您所见,i在斜杠之外,因此被解释为修饰符。
Also bear in mind that if you use a forward slash character (/) as a delimiter you must then escape further uses of / in the regular expression, if present.
另外请记住,如果使用正斜杠字符(/)作为分隔符,则必须在正则表达式中进一步使用/(如果存在)。
#4
5
The pattern must have delimiters. Delimiters can be a forward slash (/) or any non alphanumeric characters(#,$,*,...). Examples
该模式必须具有分隔符。分隔符可以是正斜杠(/)或任何非字母数字字符(#,$,*,...)。例子
$pattern = "/My name is '(.*)' and im fine/"; $pattern = "#My name is '(.*)' and im fine#";$pattern = "@My name is '(.*)' and im fine@";
#5
1
Please try with this
请尝试这个
$pattern = "/My name is '\(.*\)' and im fine/";