这个PHP Regex代码有什么问题?

时间:2021-10-29 03:25:40

The x modifier code in this tutorial Php regex tutorial gives me the following error:

本教程中的x修饰符代码Php regex教程给出了以下错误:

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';
        }
?> 

2 个解决方案

#1


2  

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

#2


1  

PHP gives you the reason for the error in the warning message: Unknown modifier ' '.

PHP为您提供了警告消息中出错的原因:Unknown modifier''。

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))
// ...

#1


2  

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

#2


1  

PHP gives you the reason for the error in the warning message: Unknown modifier ' '.

PHP为您提供了警告消息中出错的原因:Unknown modifier''。

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))
// ...