duanlu2935 2014-12-08 05:16
浏览 46

PHP搜索/过滤文本文件

As the title states, How can I search a text file using PHP and variables. I want to store the user text into a variable and use that as a search parameter in the newplaces.txt file. I know the following code does not work, but hopefully get across what I want to accomplish. I can get a match in the file, but I need help in filtering out the line to only the fields I need

<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;
?>
  • 写回答

1条回答 默认 最新

  • douxu5233 2017-04-06 12:33
    关注

    Judging from your shell code "grep -i $a $f | cut -d' '' -f10 ", you just want the 10th of the space-separated fields from the matching line. This is easily accomplished via explode, e. 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'.
    ";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程