浏览器收藏夹整理入数据库笔记!
2021-04-07 本文已影响0人
DragonersLi
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);