我有一个这样的字符串。
$string = 'Cache-Control: no-store
Content-Language: en-US-x-lvariant-USA
Vary: Accept-Encoding
{
"valid" : true,
"used" : true,
"isRecycledDomain" : false
}';
现在我想匹配 "used" : true,
的变量中。我试过用这个模式的preg_match,但还是不行。
if (preg_match('~\b\"used\" : true,\b~', $string)) {
//word matched!
} else {
//not found
}
如果你能修复这个字符串,就这么做。
否则,它的json与HTTP头有2行新的标准,然后是身体,你可以分割和解码的json成一个可行的数组与几行。
<?php
$string = 'Cache-Control: no-store
Content-Language: en-US-x-lvariant-USA
Vary: Accept-Encoding
{
"valid" : true,
"used" : true,
"isRecycledDomain" : false
}';
$string = explode("\n\n", $string, 2)[1];
$array = json_decode($string, true);
echo $array['valid'];