php搜索txt文件里的物品,发送邮件。
txt文件里物品以 id;物品名称 形式,一行一个物品:
3001;清凉一夏
3002;狐面骄人
3003;神秘猫仆
3004;吉祥如意
3005;风华正茂
3006;纯白之恋
3007;星耀长河
php代码:
<?php
if($_POST){
$key = trim($_POST['keyword']);
$return = array(array('key' => 0,'val' => '请选择'));
$file = fopen("wupin123.txt", "r");
if($key == ''){
while(!feof($file))
{
$line = fgets($file);
$txts = explode(';',$line);
if(count($txts) == 2){
$tmp = array(
'key' => $txts[0],
'val' => $txts[1]
);
array_push($return,$tmp);
}
}
}else{
while(!feof($file))
{
$line = fgets($file);
$pos = strpos($line,$key);
if($pos !== false){
$txts = explode(';',$line);
if(count($txts) == 2){
$tmp = array(
'key' => $txts[0],
'val' => $txts[1] . ' (' . $txts[0] . ')'
);
array_push($return,$tmp);
}
}
}
}
fclose($file);
echo(json_encode($return));
}else{
$return = array(array('key' => 0,'val' => '请选择'));
echo(json_encode($return));
}
|