PHP搜索/过滤文本文件

PHP搜索/过滤文本文件,php,html,shell,Php,Html,Shell,正如标题所述,如何使用PHP和变量搜索文本文件。我想将用户文本存储到一个变量中,并将其用作newplaces.txt文件中的搜索参数。我知道下面的代码不起作用,但希望能够理解我想要完成的任务。我可以在文件中找到一个匹配项,但我需要帮助筛选出行,只筛选出我需要的字段 <form method="post" action"http..."> Enter City and State <input name='place' type="text" size="10"> <

正如标题所述,如何使用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
轻松实现,例如。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";

为什么要投否决票?我更新了我的示例代码。更准确地说,我想我要做的是过滤掉匹配的行,只得到一个存储为$lat@manuydline的特定字段。你是说行吗?不清楚你想说什么是的,grep将返回匹配的整行,然后我想过滤到我想要的字段