我是一名PHP初学者,在论坛上看到了这个PHP表达式:
我的PHP版本是5.2.X()
$regex = <<<'END'
/
( [\x00-\x7F] # single-byte sequences 0xxxxxxx
| [\xC0-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
| [\xE0-\xEF][\x80-\xBF]{2} # triple-byte sequences 1110xxxx 10xxxxxx * 2
| [\xF0-\xF7][\x80-\xBF]{3} # quadruple-byte sequence 11110xxx 10xxxxxx * 3
)
| ( [\x80-\xBF] ) # invalid byte in range 10000000 - 10111111
| ( [\xC0-\xFF] ) # invalid byte in range 11000000 - 11111111
/x
END;
Run Code Online (Sandbox Code Playgroud)
这段代码是否正确?做这些怪一样(对我来说)结构<<<,'END',/,/x,和END;是什么意思?
我的PHP版本不支持nowdoc,我应该如何替换这个表达式?没有引号'END'$ regex成了NULL
我收到:
解析错误:语法错误,第X行/home/vhosts/mysite.com/public_html/mypage.php中的意外T_SL
谢谢
解析错误:语法错误,第X行/home/vhosts/mysite.com/public_html/mypage.php中的意外T_SL
这来自于周围的END.这称为nowdoc,在PHP 5.3中添加.由于您使用的是PHP 5.2,并且此正则表达式使用'\ x',因此您需要一个带引号的字符串,否则您需要转义'\'.
正则表达式作为带引号的字符串的示例,在此答案中使用:
$regex = '/
( [\x00-\x7F] # single-byte sequences 0xxxxxxx
| [\xC0-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
| [\xE0-\xEF][\x80-\xBF]{2} # triple-byte sequences 1110xxxx 10xxxxxx * 2
| [\xF0-\xF7][\x80-\xBF]{3} # quadruple-byte sequence 11110xxx 10xxxxxx * 3
)
| ( [\x80-\xBF] ) # invalid byte in range 10000000 - 10111111
| ( [\xC0-\xFF] ) # invalid byte in range 11000000 - 11111111
/x
';
Run Code Online (Sandbox Code Playgroud)
"/"和"/ x"部分是正则表达式中的控制字符."/"标记开头和结尾,x标志(PCRE_EXTENDED)的含义定义如下:http://us.php.net/manual/en/reference.pcre.pattern.modifiers.php
<<<并END称为heredoc语法 - 一种向变量引用大量数据的方法.
$mytext = <<<TXT
this is my text and it
can be many lines
etc
etc
TXT;
Run Code Online (Sandbox Code Playgroud)
这三个字符(在你的例子中为TXT,END)可以是你喜欢的任何字符,尽管它们必须是字母数字,据我所知.
阅读手册中的更多内容