php 模糊查寻TXT文本并取出一行

比如a.txt里面的内容是
上海市
北京市
上饶市呀呀呀
上饶县

假如我搜索词是【上饶】,程序自动提取a.txt里面第一次出现的【上饶市呀呀呀】这一行呢,
希望能给我个源码,本人比较菜,感谢大家了!


 header("Content-type:text/html;charset=utf-8"); 
function getStr($v){
  $file_path = "a.txt";//文件地址
  if(file_exists($file_path)){
    $fp = fopen($file_path,"r");
    $str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来
    $arr=explode("\n",str_replace("\r","",$str));//拆分为数组
    $l=count($arr);
    for($i=0;$i<$l;$i++){
      if(strpos($arr[$i],$v)!==false)return $arr[$i];
    }
  }
  return "NaN";
}

echo getStr("上饶");

$f1 = fopen('a.txt','r');
while(!feof($f1)){
$line=fgets($f1);
$line = trim($line);
$arr[] = $line;
}
$keys = array_filter($arr);

我来贴一个python的

 # -*- coding:utf-8 -*-  

if __name__ == '__main__':
    f = open(r"c:\a.txt", "r")
    line = f.readline()
    isf = 0
    while (line and isf == 0):
        if line.find("上饶") >= 0:
            print(line)
            isf = 1
        line = f.readline()

给你个思路 具体实现可以百度。循环读取每一行,拿出来了用strpos匹配