一些不清楚的 PHP 语法
我是一个 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;
这段代码正确吗?这些奇怪的(对我来说)结构,如 <<<
、'END'
、/
、/x< /code> 和
END;
是什么意思?
我的PHP版本不支持nowdoc,我应该如何替换这个表达式?不带引号 'END'
$regex 变成 NULL
我收到:
解析错误:语法错误,意外 T_SL 输入 /home/vhosts/mysite.com/public_html/mypage.php 在线 X
谢谢
I am a PHP beginner and saw on the forum this PHP expression:
My PHP version is 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;
Is this code correct? What do these strange (for me) constructions like <<<
, 'END'
, /
, /x
, and END;
mean?
My PHP version does not support nowdoc, how should I replace this expression? without quotes 'END'
$regex became NULL
I recieve:
Parse error: syntax error, unexpected
T_SL in
/home/vhosts/mysite.com/public_html/mypage.php
on line X
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这来自 END 周围的。这称为 nowdoc,是 PHP 5.3 中添加的。由于您使用的是 PHP 5.2,并且此正则表达式使用“\x”,因此您需要一个带引号的字符串,或者需要转义“\”。
正则表达式作为带引号的字符串的示例,用于此答案:
“/”和“/x”部分是正则表达式中的控制字符。 “/”标记开始和结束,x 标志 (PCRE_EXTENDED) 的含义定义在:http://us.php.net/manual/en/reference.pcre.pattern.modifiers.php
This comes from the 's around END. This is called nowdoc, which was added in PHP 5.3. Since you're using PHP 5.2, and this regex uses '\x', you'll need a quoted string or you'll need to escape the '\'s.
An example of the regex as a quoted string, used in this answer:
The "/" and "/x" portions are control characters in the regex. The "/"s mark the beginning and end, and the meaning of the x flag (PCRE_EXTENDED) is defined in: http://us.php.net/manual/en/reference.pcre.pattern.modifiers.php
<<<
和END
被称为 heredoc 语法 - 一种将大量数据引用到变量的方法。这三个字符(在您的示例中为 TXT、END)可以是您喜欢的任何字符,但据我所知,它们必须是字母数字。
阅读手册
<<<
andEND
are called heredoc syntax - a way of quoting a large amount of data to a variable.The three characters (here TXT, END in your example) can be whatever you like although they must be alphanumeric as far as I'm aware.
Read more at the manual
这是 heredoc 语法。
<代码><<< 'END' 表示它是字符串的开头,并且直到下一次出现“END”为止的所有内容都将成为字符串的一部分(甚至是换行符)。
/
和/x
实际上是正则表达式的一部分。It's heredoc syntax.
The
<<< 'END'
says that it's the start of a string and that everything until the next appearance of "END" will be part of the string (even newlines).The
/
and/x
are actually part of the regex.除了其他用户所说的heredoc语法(通常用于需要大量转义的大字符串)之外,该代码还使用“/”作为分隔符定义正则表达式。
末尾的“/x”关闭正则表达式,然后告诉正则表达式引擎以“自由间距模式”执行它。其他可能的选项是 /i(不区分大小写)或 /m(多行模式)。
您可以在此处阅读有关 PHP 正则表达式引擎的更多信息:
在 PHP 中使用正则表达式
In addition to what other users have said about it being heredoc syntax (typically used for large strings that would otherwise require a lot of escaping), the code is defining a regular expression using "/" as the deliminator.
the "/x" at the end is closing the regular expression and then telling the regex engine to execute it in "free-spacing mode". Other possible options would have been /i for case-insensitive or /m for multi-line mode.
You can read more about PHP's regex engine here:
Using Regular Expressions in PHP