Delimiter must not be alphanumeric or backslash and preg_match

后端 未结 7 1662
北荒
北荒 2020-11-22 10:20

I have this code :

$string1 = \"My name is \'Kate\' and im fine\"; 
$pattern = \"My name is \'(.*)\' and im fine\"; 
preg_match($pattern , $string1, $matches         


        
7条回答
  •  既然无缘
    2020-11-22 10:47

    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.

    $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.

    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.

提交回复
热议问题