将数组字符串与字符串进行比较
Compare an array string with a string
我正在完成一个项目,但我需要比较 HTTP 状态代码是否与另一个相同。我有一个很大的算法,我减少了它们,我发现了问题:
我有一个名为“$file_headers”的数组,在 ["Status"] 位置保存 "HTTP/1.1 301 Moved Permanently",在 if 子句中我比较 "HTTP/1.1 301 Moved Permanently"(这显然是相同),但我的代码和我说的不一样。我使用 cURL 检测 HTTP 状态代码。我的 PHP 代码如下:
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "fb.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//enable headers
curl_setopt($ch, CURLOPT_HEADER, 1);
//get only headers
curl_setopt($ch, CURLOPT_NOBODY, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$data = explode("\n",$output);
$headers_one = $data;
$headers_two = array();
$headers_two['Status'] = $data[0];
array_shift($data);
foreach($data as $part){
$middle = explode(":",$part);
$msg = null;
if(sizeof($middle) > 2){
if(strpos($middle[0],"Location") === false){
for($i = 1; $i <= sizeof($middle)-1;$i++){
$msg .= $middle[$i];
}
} else {
for($i = 1; $i <= sizeof($middle)-1;$i++){
if($i == 1){
$msg .= $middle[$i] . ":";
} else {
$msg .= $middle[$i];
}
}
}
} else if(isset($middle[1])){
$msg = $middle[1];
}
$headers_two[trim($middle[0])] = trim($msg);
}
array_pop($headers_one);
array_pop($headers_one);
array_pop($headers_two);
$file_headers = $headers_two;
if($file_headers["Status"] === ("HTTP/1.1 301 Moved Permanently") || $file_headers["Status"] === ("HTTP/1.1 301")){
echo "OK!";
} else {
echo "NO!";
}
//print all headers as array
/*echo "<pre>";
print_r($headers_one);
echo "</pre><br />";*/
echo "<pre>";
echo $file_headers["Status"];
echo "</pre>";
?>
如果有人能帮助我,我将不胜感激。谢谢,祝开发愉快!
$headers_two['Status']
是您唯一没有 trim()
ing 的元素,因此它周围有一些空格,这使得比较失败。这样做:
$headers_two['Status'] = trim($data[0]);
它会工作得很好。
我正在完成一个项目,但我需要比较 HTTP 状态代码是否与另一个相同。我有一个很大的算法,我减少了它们,我发现了问题: 我有一个名为“$file_headers”的数组,在 ["Status"] 位置保存 "HTTP/1.1 301 Moved Permanently",在 if 子句中我比较 "HTTP/1.1 301 Moved Permanently"(这显然是相同),但我的代码和我说的不一样。我使用 cURL 检测 HTTP 状态代码。我的 PHP 代码如下:
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "fb.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//enable headers
curl_setopt($ch, CURLOPT_HEADER, 1);
//get only headers
curl_setopt($ch, CURLOPT_NOBODY, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$data = explode("\n",$output);
$headers_one = $data;
$headers_two = array();
$headers_two['Status'] = $data[0];
array_shift($data);
foreach($data as $part){
$middle = explode(":",$part);
$msg = null;
if(sizeof($middle) > 2){
if(strpos($middle[0],"Location") === false){
for($i = 1; $i <= sizeof($middle)-1;$i++){
$msg .= $middle[$i];
}
} else {
for($i = 1; $i <= sizeof($middle)-1;$i++){
if($i == 1){
$msg .= $middle[$i] . ":";
} else {
$msg .= $middle[$i];
}
}
}
} else if(isset($middle[1])){
$msg = $middle[1];
}
$headers_two[trim($middle[0])] = trim($msg);
}
array_pop($headers_one);
array_pop($headers_one);
array_pop($headers_two);
$file_headers = $headers_two;
if($file_headers["Status"] === ("HTTP/1.1 301 Moved Permanently") || $file_headers["Status"] === ("HTTP/1.1 301")){
echo "OK!";
} else {
echo "NO!";
}
//print all headers as array
/*echo "<pre>";
print_r($headers_one);
echo "</pre><br />";*/
echo "<pre>";
echo $file_headers["Status"];
echo "</pre>";
?>
如果有人能帮助我,我将不胜感激。谢谢,祝开发愉快!
$headers_two['Status']
是您唯一没有 trim()
ing 的元素,因此它周围有一些空格,这使得比较失败。这样做:
$headers_two['Status'] = trim($data[0]);
它会工作得很好。