美文网首页
浏览器收藏夹整理入数据库笔记!

浏览器收藏夹整理入数据库笔记!

作者: DragonersLi | 来源:发表于2021-04-07 17:49 被阅读0次
    set_time_limit(0);
    ini_set('memory_limit', '-1');
     
    $res = file_get_contents('./urls.html');
    preg_match_all("/<a[^>]*href=['\"]([^\"]*)['\"][^>]*>(.*?)<\/a>/i",$res,$matches);
    foreach($matches[1] as $k=>$v){
         $url = parse_url($v);
         $title =   urldecode($matches[2][$k]);
         $host = urldecode($url['host']);
         $path = urldecode($url['path']);
         $result[] = "(\"{$title}\",\"{$v}\",\"{$url['scheme']}\",\"{$host}\",\"{$path}\")";
        #['url'=>$v,'scheme'=>$url['scheme'],'host'=>$url['host'],'path'=>$url['path'],'title'=>$matches[2][$k]];
    
    }
    print_R($result);die;
    $mysql_conf = [
        'db_host'    => '127.0.0.1:3306',
        'db_name'      => 'test',
        'db_user' => 'root',
        'db_pwd'  => 'root',
    ];
     
    $mysqli = @new mysqli($mysql_conf['db_host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);
    $mysqli->connect_errno && die("could not connect to the database:\n" . $mysqli->connect_error);//诊断连接错误
    $mysqli->query("set names 'utf8';");//编码转化
    $select_db = $mysqli->select_db($mysql_conf['db_name']);
    !$select_db && die("could not connect to the db:\n" .  $mysqli->error);
    $sql = "insert into url(title,url,scheme,host,path) values ".implode(',',$result);
    
     $res = $mysqli->query($sql);
    !$res &&  die("sql error:\n" . $mysqli->error);
    

    相关文章

      网友评论

          本文标题:浏览器收藏夹整理入数据库笔记!

          本文链接:https://www.haomeiwen.com/subject/ojabrxtx.html