获取域年龄(文件获取内容)的PHP脚本存在问题

获取域年龄(文件获取内容)的PHP脚本存在问题,php,file-get-contents,Php,File Get Contents,嗨,我在php中遇到了一个我不理解的问题 我制作了一个php脚本,从waybackmachine的特定域中获取域年龄,其中包含文件内容 这些域都位于一个名为Domains的数组中,并且来自用户的texfield 脚本运行良好,但仅适用于数组中的第一个域,但对于第二个域,我只从循环中获得奇怪的值或什么都没有 但我不知道为什么,我看不出有错。数组中的所有域都是正确的 谁能帮我做错事吗 //Array with Domains $domain = explode("\n",trim($_POST['u

嗨,我在php中遇到了一个我不理解的问题

我制作了一个php脚本,从waybackmachine的特定域中获取域年龄,其中包含文件内容

这些域都位于一个名为Domains的数组中,并且来自用户的texfield

脚本运行良好,但仅适用于数组中的第一个域,但对于第二个域,我只从循环中获得奇怪的值或什么都没有

但我不知道为什么,我看不出有错。数组中的所有域都是正确的

谁能帮我做错事吗

//Array with Domains
$domain = explode("\n",trim($_POST['url']));

// Print the Array for debugging
print_r($domain);



// count domains for the loop
$count = count($domain);
echo $count;

for ($i = 0; $i < $count; $i++) {

$content=file_get_contents('http://web.archive.org/cdx/search/cdx?url='.$domain[$i].'',FALSE, NULL, 1, 600);

//use the data from file_get_contents to calculate the age

preg_match('/\d+/', $content, $date); 
$startyear= substr($date[0], 0, -10);
$startmonth=  substr($date[0], 4, -8);
$actualyear= date("Y");


// calculate the year & month
$years= $actualyear- $startyear;
$month= 12-$startmonth;

//echo the Age

echo " <div style='font-size:20px;text-align:center;width:100%;height:5%;color:#25bb7f;
    font-weight: bold;'> $domain[$i]: $years Jahre und $month Monate </div>"; 

}
//具有域的数组
$domain=explode(“\n”,trim($_POST['url']);
//打印阵列以进行调试
打印(域);
//计算循环的域数
$count=count($domain);
echo$count;
对于($i=0;$i<$count;$i++){
$content=file\u get\u contents('http://web.archive.org/cdx/search/cdx?url=“.$domain[$i]”,FALSE,NULL,1600);
//使用文件\u get\u内容中的数据计算年龄
预匹配('/\d+/',$content,$date);
$startyear=substr($date[0],0,-10);
$startmonth=substr($date[0],4,-8);
$actualyear=日期(“Y”);
//计算年份和月份
$years=$actualyear-$startyear;
$month=12-$startmonth;
//呼应时代
echo“$domain[$i]:$years Jahre和$month Monate”;
}

我认为问题在于URL解码和编码。传递给
的域http://web.archive.org/cdx/search/cdx?url=“
必须完全编码。 请参见下面如何完成此操作

//Array with Domains
$domain = explode("\n",trim($_POST['url']));


# url encode all the urls/domains.
$domain = array_map(function($domain){ return urlencode($domain); }, $domain);

// Print the Array for debugging
print_r($domain);



// count domains for the loop
$count = count($domain);
echo $count;

for ($i = 0; $i < $count; $i++) {

$content=file_get_contents('http://web.archive.org/cdx/search/cdx?url='.$domain[$i].'',FALSE, NULL, 1, 600);

//use the data from file_get_contents to calculate the age

preg_match('/\d+/', $content, $date); 
$startyear= substr($date[0], 0, -10);
$startmonth=  substr($date[0], 4, -8);
$actualyear= date("Y");


// calculate the year & month
$years= $actualyear- $startyear;
$month= 12-$startmonth;

//echo the Age

$domainNonEncoded = htmlspecialchars(urldecode($domain[$i])); # get the decoded url

echo " <div style='font-size:20px;text-align:center;width:100%;height:5%;color:#25bb7f;
    font-weight: bold;'> {$domainNonEncoded}: $years Jahre und $month Monate </div>"; 

}
//具有域的数组
$domain=explode(“\n”,trim($_POST['url']);
#url编码所有url/域。
$domain=数组映射(函数($domain){return urlencode($domain);},$domain);
//打印阵列以进行调试
打印(域);
//计算循环的域数
$count=count($domain);
echo$count;
对于($i=0;$i<$count;$i++){
$content=file\u get\u contents('http://web.archive.org/cdx/search/cdx?url=“.$domain[$i]”,FALSE,NULL,1600);
//使用文件\u get\u内容中的数据计算年龄
预匹配('/\d+/',$content,$date);
$startyear=substr($date[0],0,-10);
$startmonth=substr($date[0],4,-8);
$actualyear=日期(“Y”);
//计算年份和月份
$years=$actualyear-$startyear;
$month=12-$startmonth;
//呼应时代
$domainNonEncoded=htmlspecialchars(urldecode($domain[$i]));#获取解码后的url
echo“{$domainNonEncoded}:$years jahr und$month Monate”;
}

我认为问题在于URL解码和编码。传递给
的域http://web.archive.org/cdx/search/cdx?url=“
必须完全编码。 请参见下面如何完成此操作

//Array with Domains
$domain = explode("\n",trim($_POST['url']));


# url encode all the urls/domains.
$domain = array_map(function($domain){ return urlencode($domain); }, $domain);

// Print the Array for debugging
print_r($domain);



// count domains for the loop
$count = count($domain);
echo $count;

for ($i = 0; $i < $count; $i++) {

$content=file_get_contents('http://web.archive.org/cdx/search/cdx?url='.$domain[$i].'',FALSE, NULL, 1, 600);

//use the data from file_get_contents to calculate the age

preg_match('/\d+/', $content, $date); 
$startyear= substr($date[0], 0, -10);
$startmonth=  substr($date[0], 4, -8);
$actualyear= date("Y");


// calculate the year & month
$years= $actualyear- $startyear;
$month= 12-$startmonth;

//echo the Age

$domainNonEncoded = htmlspecialchars(urldecode($domain[$i])); # get the decoded url

echo " <div style='font-size:20px;text-align:center;width:100%;height:5%;color:#25bb7f;
    font-weight: bold;'> {$domainNonEncoded}: $years Jahre und $month Monate </div>"; 

}
//具有域的数组
$domain=explode(“\n”,trim($_POST['url']);
#url编码所有url/域。
$domain=数组映射(函数($domain){return urlencode($domain);},$domain);
//打印阵列以进行调试
打印(域);
//计算循环的域数
$count=count($domain);
echo$count;
对于($i=0;$i<$count;$i++){
$content=file\u get\u contents('http://web.archive.org/cdx/search/cdx?url=“.$domain[$i]”,FALSE,NULL,1600);
//使用文件\u get\u内容中的数据计算年龄
预匹配('/\d+/',$content,$date);
$startyear=substr($date[0],0,-10);
$startmonth=substr($date[0],4,-8);
$actualyear=日期(“Y”);
//计算年份和月份
$years=$actualyear-$startyear;
$month=12-$startmonth;
//呼应时代
$domainNonEncoded=htmlspecialchars(urldecode($domain[$i]));#获取解码后的url
echo“{$domainNonEncoded}:$years jahr und$month Monate”;
}

Hi,可能您需要使用
preg\u match\u all()
?Hi,可能您需要使用
preg\u match\u all()
?谢谢,这解决了问题。现在我得到了正确的数据!这是因为域名是URL编码的。请再次查看我的答案,我编辑它是为了解决您的第二个问题。注意,我刚刚再次编辑它是为了添加
htmlspecialchars
函数。这基本上有助于通过转义某些实体字符来获得正确的浏览器输出。感谢marvin,我一直在寻找正确的解决方案,但不知道问题出在哪里:Dglad我可以提供帮助。谢谢,这就解决了问题。现在我得到了正确的数据!这是因为域名是URL编码的。请再次查看我的答案,我编辑它是为了解决您的第二个问题。注意,我刚刚再次编辑它是为了添加
htmlspecialchars
函数。这基本上有助于通过转义某些实体字符来获得正确的浏览器输出。感谢marvin,我一直在寻找正确的解决方案,但不知道问题出在哪里:Dglad我可以提供帮助。