Home > Backend Development > PHP Tutorial > foreach - PHP 导入文本文件的正则表达式提取部分内容 变成数组 然后存入到mysql数据库中

foreach - PHP 导入文本文件的正则表达式提取部分内容 变成数组 然后存入到mysql数据库中

WBOY
Release: 2016-06-06 20:49:46
Original
1071 people have browsed it

想使用PHP正则表达式读取下面的文本文件,将date,time读取 并加上当前的年 写成: 2013-07-03 17:22,还有NMBR的电话号码读出,并生成一个循环的mysql语句 并存入到mysql数据库中.

<code>RING

RING

DATE = 0703 

TIME = 1722
NMBR = 13332415858

RING

RING

DATE = 0704 

TIME = 0812 

NMBR = 04124834444

RING

RING
</code>
Copy after login
Copy after login

变成:

<code>insert into calllog (datetime,phonenumber ) values ('2013-07-03 17:22','13332415858');
insert into calllog (datetime,phonenumber ) values ('2013-07-04 08:12','04124834444');
</code>
Copy after login
Copy after login

回复内容:

想使用PHP正则表达式读取下面的文本文件,将date,time读取 并加上当前的年 写成: 2013-07-03 17:22,还有NMBR的电话号码读出,并生成一个循环的mysql语句 并存入到mysql数据库中.

<code>RING

RING

DATE = 0703 

TIME = 1722
NMBR = 13332415858

RING

RING

DATE = 0704 

TIME = 0812 

NMBR = 04124834444

RING

RING
</code>
Copy after login
Copy after login

变成:

<code>insert into calllog (datetime,phonenumber ) values ('2013-07-03 17:22','13332415858');
insert into calllog (datetime,phonenumber ) values ('2013-07-04 08:12','04124834444');
</code>
Copy after login
Copy after login

<code><?php //date_default_timezone_set('Asia/Shanghai');
$t = file_get_contents('log.txt');
$c = preg_match_all('/= (.*?)$/m', $t, $m);
$sql = array();
for($i=0;$i<$c;$i+=3) {
    $d = date('Y-m-d H:i', strtotime('2013'.trim($m[1][$i]).trim($m[1][$i+1])));
    $n = $m[1][$i+2];
    //replace the next code with yours
    $sql[] = "INSERT INTO calllog (date,phonenumber) VALUES ('$d', '$n')";
}
</code></code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template