php读取文本写入数据库,php读取txt文本文档数据库转入mysql数据库
滿足固定格式的txt文檔內(nèi)容轉(zhuǎn)為mysql數(shù)據(jù)庫(kù),與最近寫的php實(shí)現(xiàn)SEO偽原創(chuàng)同義詞替換函數(shù)
一文讀取數(shù)據(jù)庫(kù)時(shí)采用的方法一致。
$txt = file_get_contents('data.txt');
$txt = str_replace( "\r", "",$txt);
$txt = preg_split('/\n/', $txt, -1, PREG_SPLIT_NO_EMPTY);
foreach($txt as $k=>$v){
if($k!=0){//判斷是否為第一行
$str = explode('|',$v);//“|”數(shù)據(jù)分隔符
$sql = "INSERT INTO table
(id,content1,content2) VALUES ('".$str[0]."','".$str[1]."','".$str[2]."')";
}
echo $sql.'
';//將sql語句用于mysql數(shù)據(jù)庫(kù)操作即可。
}
?>
數(shù)據(jù)data.txt格式如下:
1|內(nèi)容1|內(nèi)容2
2|內(nèi)容3|內(nèi)容4
……
總結(jié)
以上是生活随笔為你收集整理的php读取文本写入数据库,php读取txt文本文档数据库转入mysql数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php获取表单信息的代码_php 表单数
- 下一篇: java代码如何避免死锁,Java可重入