您的位置:首页 > 编程语言 > PHP开发

php 读取TXT文本生成 想要的数据格式

2017-01-20 13:59 471 查看
控制器





public function index()
{

$_rows = M('tb')->where(['areaType' => 2])->select();
foreach ($_rows as $k => $v) {
echo "pro_array[" . $v['areaCode'] . "]" . "=" . '"' . $v['areaName'] . '";';
echo '<br>';
echo "city_array[" . $v['areaCode'] . "]" . "=" . "[]";
echo '<br>';
echo "city_array[" . $v['areaCode'] . "]" . "[0]" . "=" . '"' . "请选择" . '";';
echo '<br>';
$_rows2 = M('tb')->where(['areaType' => 3, 'areaParentId' => $v['areaCode']])->select();
foreach ($_rows2 as $k2 => $v2) {
echo "city_array[" . $v['areaCode'] . "]" . "[" . $v2['areaCode'] . "]" . "=" . '"' . $v2['areaName'] . '";';
echo '<br>';
echo "l_arr[" . $v2['areaCode'] . "]" . "=" . '"' . $v2['areaName'] . '";';
echo '<br>';
echo "qu_arr[" . $v2['areaCode'] . "]" . "=" . "[]" . ";";
echo '<br>';
echo "qu_arr[" . $v2['areaCode'] . "]" . "[0]" . "=" . '"' . "请选择" . '";';
echo '<br>';

$_r = M('tb')->where(['areaType' => 4, 'areaParentId' => $v2['areaCode']])->find();
if ($_r) {
$_rows3 = M('tb')->where(['areaType' => 4, 'areaParentId' => $v2['areaCode']])->select();
foreach ($_rows3 as $k3 => $v3) {
echo "qu_arr[" . $v2['areaCode'] . "]" . "[" . $v3['areaCode'] . "]" . "=" . '"' . $v3['areaName'] . '";';
echo '<br>';
}

} else {
$_rows3 = M('tb')->where(['areaType' => 5, 'areaParentId' => $v2['areaCode']])->select();
foreach ($_rows3 as $k3 => $v3) {
echo "qu_arr[" . $v2['areaCode'] . "]" . "[" . $v3['areaCode'] . "]" . "=" . '"' . $v3['areaName'] . '";';
echo '<br>';
}

}

}

}

}











<?php
header("Content-type: text/html; charset=gb2312");
$handle = @fopen("111.txt", "r");

function zhongwen($text){

$aaaa= substr($text,6,100)."<br>"; //获取每一行的第2个参数
//把GB2312编码转化为 UTF-8编码
$str = mb_convert_encoding($aaaa, 'UTF-8', 'GB2312');
//正则匹配
preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $str, $matches);
//把匹配到的数组连接为字符串
$str = implode('', $matches[0]);
//把UTF-8编码转化为 GB2312编码
$text2 = mb_convert_encoding($str, 'GB2312', 'UTF-8');

return $text2;
}

if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
$text =$buffer;  //获取一行数据
$aaaa = substr($text,6,1000);          //获取每1行的第一个参数
$text1 = substr($text,0,6);          //获取每1行的第一个参数
$text2 = zhongwen($text);
$par = "/[\x80-\xff]/";
$text3 = preg_replace($par,"",$aaaa)."<br>";
if($text3==1){
echo "pro_array"."[$text1]"."=".$text2.";<br>";
echo "city_array[$text1]=[];"."<br>";
echo "city_array[$text1][0]="."请选择".";<br>";

/*  while ($handle) {
$buffer = fgets($handle, 4096);
$text =$buffer;  //获取一行数据
$aaaa = substr($text,6,1000);          //获取每1行的第一个参数
$text1 = substr($text,0,6);          //获取每1行的第一个参数
$text2 = zhongwen($text);
$par = "/[\x80-\xff]/";
$text3 = preg_replace($par,"",$aaaa)."<br>";

//return $c;
}*/
}

}
fclose($handle);  // 关闭
}

function city($city,$handle){
$c = [];
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
$text =$buffer;  //获取一行数据
$aaaa = substr($text,6,1000);          //获取每1行的第一个参数
$text1 = substr($text,0,6);          //获取每1行的第一个参数
$text2 = zhongwen($text);
$par = "/[\x80-\xff]/";
$text3 = preg_replace($par,"",$aaaa)."<br>";
if($text3==$city){
echo  "city_array[$city][$text1]=".$text2.";<br>";
}
//return $c;
}
fclose($handle);  // 关闭
}

}

?>


<?

error_reporting(E_ALL ^ E_DEPRECATED);

header("Content-type: text/html; charset=gb2312");

$handle = @fopen("111.txt", "r");

$conn = @ mysql_connect("localhost", "root", "root") or die("数据库链接错误");

mysql_select_db("test", $conn);

mysql_query("set names 'GBK'"); //使用GBK中文编码;

function zhongwen($text){

 $aaaa= substr($text,6,100)."<br>"; //获取每一行的第2个参数

 //把GB2312编码转化为 UTF-8编码

 $str = mb_convert_encoding($aaaa, 'UTF-8', 'GB2312');

 //正则匹配

 preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $str, $matches);

 //把匹配到的数组连接为字符串

 $str = implode('', $matches[0]);

 //把UTF-8编码转化为 GB2312编码

 $text2 = mb_convert_encoding($str, 'GB2312', 'UTF-8');

 return $text2;

}

if ($handle) {

 while (!feof($handle)) {

  $buffer = fgets($handle, 4096);

  $text =$buffer;  //获取一行数据

  $aaaa = substr($text,6,1000);          //获取每1行的第一个参数

  $text1 = substr($text,0,6);          //获取每1行的第一个参数

  $text2 = zhongwen($text);

  $par = "/[\x80-\xff]/";

  $text3 = preg_replace($par,"",$aaaa);

  

  //城市

   if($text3==1){   

     echo "pro_array"."[$text1]"."=".$text2.";<br>";

     echo "city_array[$text1]=[];"."<br>";

     echo "city_array[$text1][0]="."xuanze".";<br>";

     

     $_sql = "SELECT * FROM qiuyang WHERE c = '$text1'";

     $_result = mysql_query($_sql);

     while($_rows = mysql_fetch_array($_result,MYSQL_ASSOC)){

       $c = $_rows['c'];

       $a = $_rows['a'];

      echo "city_array[$c][$a]=".$_rows['b'].";<br>";

      

      //区县  

      echo "l_arr[$a]=".$_rows['b'].";<br>";

      echo "qu_arr[$a]="."[]".";<br>";

      echo "qu_arr[$a][0]="."xuanze".";<br>";

   

         $_sql2 = "SELECT * FROM qiuyang WHERE c = '$a'";

         $_result2 = mysql_query($_sql2);

      while($_rows2 = mysql_fetch_array($_result2,MYSQL_ASSOC)){

       $c2 = $_rows2['c'];

       $a2 = $_rows2['a'];

        echo "qu_arr[$c2][$a2]=".$_rows2['b'].";<br>";

       

      }

     }

    

    }

   

 }

 fclose($handle);  // 关闭

}

?>

插入数据

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?

error_reporting(E_ALL ^ E_DEPRECATED);

header("Content-type: text/html; charset=gb2312");

$handle = @fopen("111.txt", "r");

$conn = @ mysql_connect("localhost", "root", "root") or die("数据库链接错误");

mysql_select_db("test", $conn);

mysql_query("set names 'GBK'"); //使用GBK中文编码;

function zhongwen($text){

 $aaaa= substr($text,6,100)."<br>"; //获取每一行的第2个参数

 //把GB2312编码转化为 UTF-8编码

 $str = mb_convert_encoding($aaaa, 'UTF-8', 'GB2312');

 //正则匹配

 preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $str, $matches);

 //把匹配到的数组连接为字符串

 $str = implode('', $matches[0]);

 //把UTF-8编码转化为 GB2312编码

 $text2 = mb_convert_encoding($str, 'GB2312', 'UTF-8');

 return $text2;

}

if ($handle) {

 while (!feof($handle)) {

  $buffer = fgets($handle, 4096);

  $text =$buffer;  //获取一行数据

  $aaaa = substr($text,6,1000);          //获取每1行的第一个参数

  $text1 = substr($text,0,6);          //获取每1行的第一个参数

  $text2 = zhongwen($text);

  $par = "/[\x80-\xff]/";

  $text3 = preg_replace($par,"",$aaaa);

  

  

  //$_sql =  "INSERT INTO qiuyang ( a,b,c) VALUES ('$text1','$text2', '$text3' )";

 // $_result = mysql_query($_sql);

  

  

  //echo $text1;

  //echo $text2;

  //echo $text3;

  

 }

 fclose($handle);  // 关闭

}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: