2014-12-08 75 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";