PHP读取远程txt文档到数组并实现遍历

代码如下

<?php  
$txt = "http://www.xxx.cn/sitemap.txt"; //读取远程txt;
//是否存在
if(file_get_contents($txt)){
$data = file($txt); //将文件存放在一个数组中;
$num = count($data); //条数;
$id = mt_rand(0,$num-1); //随机数字;
$url = chop($data[$id]); //显示第几行数据,并去除空格;
echo "$url"; //随机显示一条域名
}
echo "</br>";
echo "zongtiaoshu:"."$num";
echo "</br>";
for ($i=0; $i < $num ; $i++) {
  echo $data[$i]."</br>";
}
//header("Location: $url"); //开始随机跳转
?>

主要用于遍历网站在线sitemap.txt内容读取。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持来客网。