json_decode:'Syntax error' 来自 HEREDOC 字符串的有效 JSON
json_decode: 'Syntax error' for valid JSON from HEREDOC string
我有下面的 JSON 代码。根据 one or two JSON 验证器,它是有效的 JSON.
{
"patterns": {
"email": "/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i",
"phone": "/(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?/"
}
}
但是,当我尝试使用 PHP 中的 json_decode
函数进行解码时,我得到了 'Syntax Error'。这是我的 PHP 代码:
const JSON_CONFIG = <<<JSON
{
"patterns": {
"email": "/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i",
"phone": "/(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?/"
}
}
JSON;
$config = json_decode(mb_convert_encoding(JSON_CONFIG, "UTF-8"), true); // Tried called trim but it made no difference
echo 'json_last_error_msg() => ' . json_last_error_msg() . PHP_EOL;
print_r($config); // Doesn't get to run
自己试试:https://repl.it/@DanStevens/PHP-jsondecode-Syntax-Error
有什么想法 json_decode
不喜欢这个有效的 JSON?跟使用HEREDOC有关系吗?
反斜杠充当 PHP 转义序列,而不是 JSON 转义序列。为防止 PHP 转义,请将您的 heredoc 开始标记用单引号引起来:
const JSON_CONFIG = <<<'JSON'
{
"patterns": {
"email": "/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i",
"phone": "/(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?/"
}
}
JSON;
var_dump(JSON_CONFIG);
echo PHP_EOL;
$config = json_decode(mb_convert_encoding(JSON_CONFIG, "UTF-8"), true); // Tried called trim but it made no difference
echo 'json_last_error_msg() => ' . json_last_error_msg() . PHP_EOL;
echo 'json_last_error() => ' . json_last_error() . PHP_EOL;
print_r($config);
我有下面的 JSON 代码。根据 one or two JSON 验证器,它是有效的 JSON.
{
"patterns": {
"email": "/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i",
"phone": "/(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?/"
}
}
但是,当我尝试使用 PHP 中的 json_decode
函数进行解码时,我得到了 'Syntax Error'。这是我的 PHP 代码:
const JSON_CONFIG = <<<JSON
{
"patterns": {
"email": "/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i",
"phone": "/(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?/"
}
}
JSON;
$config = json_decode(mb_convert_encoding(JSON_CONFIG, "UTF-8"), true); // Tried called trim but it made no difference
echo 'json_last_error_msg() => ' . json_last_error_msg() . PHP_EOL;
print_r($config); // Doesn't get to run
自己试试:https://repl.it/@DanStevens/PHP-jsondecode-Syntax-Error
有什么想法 json_decode
不喜欢这个有效的 JSON?跟使用HEREDOC有关系吗?
反斜杠充当 PHP 转义序列,而不是 JSON 转义序列。为防止 PHP 转义,请将您的 heredoc 开始标记用单引号引起来:
const JSON_CONFIG = <<<'JSON'
{
"patterns": {
"email": "/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i",
"phone": "/(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?/"
}
}
JSON;
var_dump(JSON_CONFIG);
echo PHP_EOL;
$config = json_decode(mb_convert_encoding(JSON_CONFIG, "UTF-8"), true); // Tried called trim but it made no difference
echo 'json_last_error_msg() => ' . json_last_error_msg() . PHP_EOL;
echo 'json_last_error() => ' . json_last_error() . PHP_EOL;
print_r($config);