簡體   English   中英

PHP搜索/過濾文本文件

[英]PHP to search/filter text file

如標題所述,如何使用PHP和變量搜索文本文件。 我想將用戶文本存儲到變量中,並將其用作newplaces.txt文件中的搜索參數。 我知道以下代碼不起作用,但希望能克服我想要完成的工作。 我可以在文件中找到一個匹配項,但是我需要幫助來過濾掉僅我需要的字段的行

<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;
?>

從您的shell代碼"grep -i $a $f | cut -d' '' -f10 " ,您只需要匹配行中以空格分隔的字段的第10個。 這很容易通過explode來完成,例如

$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";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM