[英]Escaping characters in regular expressions in PHP
The following returns true: 以下返回true:
$bool1 = preg_match("/5/", "Your bill is $5.00 dude");
var_dump($bool1);
I expect this because '5' is in the string.But the following returns false: 我期待这个,因为'5'在字符串中。但是以下返回false:
$bool1 = preg_match("/\$5\./", "Your bill is $5.00 dude");
var_dump($bool1);
I thought I was escaping the $
correctly so that it would look for $5
in the string and find it, but no. 我以为我正在逃避$
正确,所以它会在字符串中查找$5
并找到它,但不是。 Can someone explain? 谁能解释一下? Thanks. 谢谢。
使用单引号来防止PHP误解任何转义:
$bool1 = preg_match('/\$5\./', "Your bill is $5.00 dude");
你应该使用preg_quote()函数进行正确的转义
$bool1 = preg_match('/\$5\./', "Your bill is $5.00 dude");
var_dump($bool1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.