2014-12-08 77 views
0

正如标题所述,如何使用PHP和变量搜索文本文件。我想将用户文本存储到变量中,并将其用作newplaces.txt文件中的搜索参数。我知道以下代码不起作用,但希望能够完成我想要完成的任务。我可以在文件中匹配,但我需要在该行筛选出来只有我需要PHP搜索/过滤文本文件

<form method="post" action"http..."> 
Enter City and State <input name='place' type="text" size="10"> 
<input type="submit" value="Submit"> 
</form> 
<?php 
$f="/newplaces.txt"; 
$o=file($f); 

$a=$_POST['place']; 

$c=preg_grep("/\b$a\b/", $o); 
echo implode($c, ' '); 

$lat=exec("grep -i $a $f | cut -d' '' -f10 "); //Need help with filtering the match 
echo $lat; 
?> 
+0

为什么downvote?问一个问题,而不是 – swam 2014-12-08 05:18:14

+0

参考此网址https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=php+search+text+in+file – manuyd 2014-12-08 05:23:31

+0

Ive更新了我的示例代码。为了更精确地解决我的问题,我想我要做的就是过滤匹配的行,以获得仅存储为$ lat @manuyd – swam 2014-12-08 05:30:32

回答

0

从你的shell代码"grep -i $a $f | cut -d' '' -f10 "来看领域的帮助下,你只是想从空间分隔的字段10日匹配线。这很容易通过explode完成,例如, G。

$fields = explode(' ', $c[1]); # $c[1] is the first matching line from the preg_grep() 
$lat = $fields[10];    # $fields[10] now is the 10th field from that line 
echo "The 10th field is '$lat'.\n";