我正在遍历一系列 CURL 返回的 http 标头的每一行,试图检测一个结束和下一个开始的时间。我知道 http 标头以空行结尾,但是在 php 中用什么字符来表示这个换行符?我试过了,\n
但它似乎不起作用。我当然可能做错了什么。
什么字符用于表示用于终止标题的换行符?
这是我现有的代码:
$redirect = '';
$regs = '';
foreach ($curl_response as $line)
{
if ($line != "\n")
{ # line is not a linebreak, so we're still processing a header block
if (preg_match("(HTTP/[0-9]\.[0-9] [0-9]{3} .*)",$line))
{ # line is the status code
# highlight the outputted line
$output .= "<b style='background: yellow;'>$line</b>";
}
elseif (preg_match("/^Location: (.*)$/m",$line,$regs))
{ # the line is a location header, so grab the location being redirected to
# highlight the outputted line
$output .= "<b style='background: purple; color: white;'>$line</b>";
$redirect = $regs[1];
}
else
{ # some other header, record to output
$output .= $line;
}
}
else
{ # we've reached a line break, so we're getting to a new block of redirects
$output .= "\nreached line break\n";
if ($redirect != '')
{ # if we recorded a redirect above, append it to output
$output .= "\n\nRedirecting to $redirect\n\n";
$redirect = '';
}
}
}
echo $output;
已解决- 原来这\r
是我应该匹配的。很奇怪。不确定这是否每个站点都会更改,或者是否是 curl 中设置的内容。到目前为止,它\r
在我尝试过的所有网站上。
编辑2:Doh。我认为这是因为为了将标题放入一系列行中,我在\n
. 所以也许\r\n
现在只是\r
...
$c = explode("\n",$content);