我有一个包含多条记录的文本文件。我想搜索姓名和日期,例如,如果我输入JULIUS CESAR
作为名称,则会提取有关JULIUS
的所有数据。如果我只想提取信息怎么办?
Record number: 1
Date: 08-Oct-08
Time: 23:45:01
Name: JULIUS CESAR
Address: BAGUIO CITY, Philippines
Information:
I lived in Peza Loakan, Bagiou City
A Computer Engineering student
An OJT at TIPI.
23 years old.
Record number: 2
Date: 09-Oct-08
Time: 23:45:01
Name: JOHN Castro
Address: BAGUIO CITY, Philippines
Information:
I lived in Peza Loakan, Bagiou City
A Electronics Comm. Engineering Student at SLU.
An OJT at TIPI.
My Hobby is Programming.
Record number: 3
Date: 08-Oct-08
Time: 23:45:01
Name: CESAR JOSE
Address: BAGUIO CITY, Philippines
Information:
Hi,,
I lived Manila City
A Computer Engineering student
Working at TIPI.
答案 0 :(得分:2)
如果每个条目一行,您可以使用正则表达式,例如:
$name = "JULIUS CESAR";
然后使用:
/$name/i
测试每一行是否与“JULIUS CESAR”有关。然后,您只需使用以下正则表达式来提取信息(一旦找到该行):
/Record number: (\d+) Date: (\d+)-(\w+)-(\d+) Time: (\d+):(\d+):(\d+) Name: $name Address: ([\w\s]+), ([\w\s]+?) Information: (.+?)$/i
$ 1 =记录号
$ 2- $ 4 =日期
$ 5- $ 7 =时间
$ 6 =地址
$ 7 =评论
我会写一个代码示例,但我的perl生锈了。我希望这会有所帮助:)
答案 1 :(得分:-1)
在PHP中,您可以运行SQL select语句,如:
“SELECT * WHERE name
LIKE'JULIUS%';”
PHP的本机方面您可以在关联数组中获得所有结果。我很确定它是按行顺序排序的。然后你可以做这样的事情:
echo implode(“”,$ whole_row);
希望这就是你要找的东西!