【问题标题】:Searching a name in a file txt using PHP使用 PHP 在文件 txt 中搜索名称
【发布时间】:2019-08-27 04:13:24
【问题描述】:

我正在尝试创建一个搜索功能来搜索包含在 txt 文件中的数据中的名称。

我有这样的数据:

dimitri, 1998, php
nikolai, 1998, php
yuri, 1998, php
alyosha, 1998, php

我想出了一个想法,将这些数据更改为这样的数组:

Array
(
    [0] => dimitri, 1998, php
    [1] => nikolai, 1998, php
    [2] => yuri, 1998, php
    [3] => alyosha, 1998, php
)

然后再多分成为多维

Array
(
    [0] => dimitri
       Array(
           [0] => 1998
           [1]=> php
    [1] => nikolai
Array(
           [0] => 1998
           [1]=> php
    [2] => yuri
Array(
           [0] => 1998
           [1]=> php
    [3] => alyosha
Array(
           [0] => 1998
           [1]=> php
)

这样我就可以通过键搜索名称。 现在我不知道该怎么办了。我尝试使用foreach() 函数来分解数组中的值,但是它不起作用,它产生了另一个问题,数组只显示了一些字符。

尝试

$array = array();
$split = explode('\n', file_get_contents($file));
foreach ($split as $content){
    $array = array_filter(array_map("trim", explode("\n", $content)));
    $array1 = array();
    $split2 = explode(", ", $array);
    foreach($array as $row){
        $array1[$row[1]][$row[2]][]=$row[0];
    }
}

HTML

<form action="search.php" method="POST">
    <input name="search_function" type="text" placeholder="Search who you want">
    <input type="submit" name="search" value="Search">
</form>

关于我的搜索,我正在考虑使用 post,如果在搜索输入中正确输入数据,数据将显示。

【问题讨论】:

  • 这可能会有所帮助 - stackoverflow.com/a/12315645/5284695
  • 你为什么要在\n 两次次拆分它? (好吧,您只是在尝试,因为您在第一次爆炸中使用的'\n' 不是 换行符。)您将其读入数组的整个逻辑太复杂了 - 请改用file(请务必注意手册中关于处理换行符的内容),然后从那里获取。
  • @AakashMartand 我认为这个解决方案只有在你有一个包含键和值的数组时才有效。我有一个包含一个值的数组,其中包含许多我要拆分的信息,并且搜索可以做到,对吗?我想不出任何解决方案
  • 您的数据似乎是 csv 格式。将您的数据放在一个变量中(比如说 $datas),然后:array_map('str_getcsv', explode('\n', $datas))

标签: php html arrays string search


【解决方案1】:

你可以尝试关注

/* read data from text file, line by line */
$arr = []; // file contents go into this array
$handle = fopen('test.txt', 'r');
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        $arr[] = explode(',', $line);
    }
    fclose($handle);
} else {
    echo 'error occured';
}

/* echo the form */
$form = <<<HEREDOC
<form action="#" method="POST">
    <input name="search_function" type="text" placeholder="Search who you want">
    <input type="submit" name="search" value="Search">
</form>
HEREDOC;
echo $form;

/* the search logic */
$found = false;
$count = 0; // count # of hits
if(isset($_POST['search'])) {
    foreach($arr as $key => $value) {
        foreach ($value as $key => $value) {
            if ($value == $_POST['search_function']) {
                $count++;
                $found = true;
                break 2;
            }
        }
    }
}
/* result */
$message = ($found === true) ? "found " . $_POST['search_function'] . " (" . $count . ") occurrence." : 'nothing found.';
echo $message;

【讨论】:

    【解决方案2】:

    您的文件似乎是 CSV 格式(或至少其内容是),因此请考虑以下代码:

    $datas = array_map('str_getcsv', file($pathToYourFile));
    

    它将您的文件转换为数组。

    https://www.php.net/manual/fr/function.file.php https://www.php.net/manual/fr/function.str-getcsv.php

    【讨论】:

      【解决方案3】:

      还有一个PHP例子,会显示多条匹配的行:

      <?php
      $file = 'somefile.txt';
      $searchfor = 'name';
      
      // the following line prevents the browser from parsing this as HTML.
      header('Content-Type: text/plain');
      
      // get the file contents, assuming the file to be readable (and exist)
      $contents = file_get_contents($file);
      // escape special characters in the query
      $pattern = preg_quote($searchfor, '/');
      // finalise the regular expression, matching the whole line
      $pattern = "/^.*$pattern.*\$/m";
      // search, and store all matching occurences in $matches
      if(preg_match_all($pattern, $contents, $matches)){
         echo "Found matches:\n";
         echo implode("\n", $matches[0]);
      }
      else{
         echo "No matches found";
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-11
        • 2017-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-15
        相关资源
        最近更新 更多