PIPIONE
这是之前把TXT文件当数据库用的时候封装的一个对TXT文件读写的类库,其中可以把TXT文本转成数组,然后遍历数组去请求网址就可以了。参考源码给你。
namespace model;
use think\Model;
Class SensitiveModel extends Model{
private static $txtpath;
private static $stop = FALSE;
private static $wrong;
//文件类型检查
public function name_check($path){
$name = substr($path,-4);
if($name == ".txt"){
return TRUE;
}else{
return FALSE;
}
}
//打开文件
public function open($path,$data=FALSE){
if(self::name_check($path)){
if(!file_exists($path)){
if($data){
$this->create($path,$data);
}else{
$this->create($path);
}
self::$txtpath = $path;
return TRUE;
}else{
self::$txtpath = $path;
return TRUE;
}
}else{
self::$stop = TRUE;
self::$wrong = "文件类型错误";
return FALSE;
}
}
//读文件
public function read($echo = FALSE,$blank = FALSE){
if(!self::$stop && self::$txtpath){
$data = file_get_contents(self::$txtpath);
if($blank){
$data = str_replace("\n","
",$data);
}
if($echo){
echo $data;
}else{
return $data;
}
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "请先选择文件";
}
return FALSE;
}
}
//写文件
public function createtxt($path,$data=FALSE){
if(self::name_check($path)){
if(file_exists($path)){
self::$stop = TRUE;
self::$wrong = "文件已存在";
return FALSE;
}else{
file_put_contents($path,$data);
self::$txtpath = $path;
return TRUE;
}
}else{
self::$stop = TRUE;
self::$wrong = "文件类型错误";
return FALSE;
}
}
//修改文件内容
public function change($data){
if(!self::$stop && self::$txtpath){
file_put_contents(self::$txtpath,$data);
return TRUE;
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "请先选择文件";
}
return FALSE;
}
}
//追加内容到最后
public function appendtxt($data,$line=FALSE){
if(!self::$stop && self::$txtpath){
if($line && $this->read()){
$data = "\r\n".$data;
}
file_put_contents(self::$txtpath,$data, FILE_APPEND);
return TRUE;
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "请先选择文件";
}
return FALSE;
}
}
//删除文件
public function delete(){
if(!self::$stop && self::$txtpath){
unlink(self::$txtpath);
return TRUE;
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "请先选择文件";
}
return FALSE;
}
}
//错误捕获,能获取到所有操作出现的错误
public function wrong(){
if(self::$wrong){
return self::$wrong;
}
}
//获取指定行内容
public function get_line($line_number){
if(!self::$stop && self::$txtpath){
$data = $this->read();
$lines = explode("\n",$data);
if(array_key_exists($line_number,$lines)){
return $lines[$line_number];
}else{
self::$stop = TRUE;
self::$wrong = "指定行不存在";
return FALSE;
}
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "请先选择文件";
}
return FALSE;
}
}
//删除指定行内容
public function del_line($line_number){
if(!self::$stop && self::$txtpath){
$data = $this->read();
$lines = explode("\n",$data);
if(array_key_exists($line_number,$lines)){
unset($lines[$line_number]);
$newdata = implode("\n",$lines);
$this->change($newdata);
return TRUE;
}else{
self::$stop = TRUE;
self::$wrong = "指定行不存在";
return FALSE;
}
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "请先选择文件";
}
return FALSE;
}
}
//修改指定行内容
public function change_line($line_number,$text){
if(!self::$stop && self::$txtpath){
$data = $this->read();
$lines = explode("\n",$data);
if(array_key_exists($line_number,$lines)){
$lines[$line_number] = $text;
$newdata = implode("\n",$lines);
$this->change($newdata);
return TRUE;
}else{
self::$stop = TRUE;
self::$wrong = "指定行不存在";
return FALSE;
}
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "请先选择文件";
}
return FALSE;
}
}
//将txt读取出来并变成arr
public function txt2arr($path){
if(self::name_check($path)){
$data = array();
if(file_exists($path)){
$stream = file_get_contents($path);
$str_encoding = mb_convert_encoding($stream, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
$data = explode("\r\n", $str_encoding);
foreach ($data as &$row) {
$row = trim($row);
}
unset($row);
}
return $data;
}
}
//将arr数组按行写入txt,并删除之前内容
public function arr2txt($path,$arr){
$arr = implode("\r\n",$arr);
if($this->name_check($path)){
$res = file_put_contents($path,$arr);
if($res){
return true;
}else{
return false;
}
}else{
return false;
}
}
//将arr数组按行写入txt,不删除之前内容
public function arrappendtxt($path,$arr){
if($this->name_check($path)){
$arr = implode("\r\n",$arr);
$str = "\r\n";
file_put_contents($path,$str,FILE_APPEND);
$res = file_put_contents($path,$arr,FILE_APPEND);
if($res){
return true;
}else{
return false;
}
}else{
return false;
}
}
//用PHP对数据进行分页
public function pagenate($arr,$page=1,$size=10){
//传入需要分页显示的数据,页码,每页显示条数
$pages = ceil(count($arr)/$size);
$newarr = array_slice($arr,($pages-1)*$size,$size);
for($i=0;$i
$data[$i] = $arr[$i];
}
return $data;
}
}
楼主改改应该就能用了。