[英]What's wrong with this PHP Regex code?
The x modifier code in this tutorial Php regex tutorial gives me the following error: 本教程Php regex教程中的x修饰符代码给我以下错误:
Warning: preg_match() [function.preg-match]: Unknown modifier ' ' in C:\xampp\htdocs\validation\test.php on line 16 Pattern not found
What's wrong with it? 它出什么问题了?
<?php
// create a string
$string = 'sex'."\n".'at'."\n".'noon'."\n".'taxes'."\n";
// create our regex using comments and store the regex
// in a variable to be used with preg_match
$regex ="
/ # opening double quote
^ # caret means beginning of the string
noon # the pattern to match
/imx
";
// look for a match
if(preg_match($regex, $string))
{
echo 'Pattern Found';
}
else
{
echo 'Pattern not found';
}
?>
You have an extra newline in the modifiers because the terminating quote is on a new line after imx
, that is why you are seeing unknown modifier ' '
您在修饰符中有一个额外的换行符,因为终止报价在
imx
之后换行了,这就是为什么您看到未知的修饰符' '
Try changing it to this: 尝试将其更改为此:
$regex ="
/ # opening double quote
^ # caret means beginning of the string
noon # the pattern to match
/imx"; // move "; to same line as /imx
PHP gives you the reason for the error in the warning message: Unknown modifier ' '
. PHP在警告消息中为您提供了错误的原因: 未知修饰符
' '
。
Obviously you are not allowed to have white space in the modifier list after the ending delimiter /
in your pattern. 显然,在模式的结尾定界符
/
后,不允许在修饰符列表中留空格。 You can remove this white space with the trim()
function: 您可以使用
trim()
函数删除此空白:
if (preg_match(trim($regex), $string))
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.