[英]What's wrong with this PHP Regex code?
本教程Php regex教程中的x修飾符代碼給我以下錯誤:
Warning: preg_match() [function.preg-match]: Unknown modifier ' ' in C:\xampp\htdocs\validation\test.php on line 16 Pattern not found
它出什么問題了?
<?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';
}
?>
您在修飾符中有一個額外的換行符,因為終止報價在imx
之后換行了,這就是為什么您看到未知的修飾符' '
嘗試將其更改為此:
$regex ="
/ # opening double quote
^ # caret means beginning of the string
noon # the pattern to match
/imx"; // move "; to same line as /imx
PHP在警告消息中為您提供了錯誤的原因: 未知修飾符' '
。
顯然,在模式的結尾定界符/
后,不允許在修飾符列表中留空格。 您可以使用trim()
函數刪除此空白:
if (preg_match(trim($regex), $string))
// ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.