1

我有这样的php输出:

data00,data01,data02,data03, , , and so on
data10,data11,data12,data13, , , and so on
data20,data21,data22,data23, , , and so on
(and so on in rows)

以及生成此数据的此 php 代码。

<?php
$searchthis = $_GET["sea1"];
$matches = array();

$handle = @fopen("sources.csv", "r");
if ($handle)
{
    while (!feof($handle))
    {
        $buffer = fgets($handle);
        if(strpos($buffer, $searchthis) !== FALSE)
            $matches[] = $buffer;
    }
    fclose($handle);
}

//show results:
print_r($matches);

现在我想在页面某处的特定列上显示特定行,如果我使用:

<?php echo $matches[0]; ?>

它会告诉我整行,而不是我想要的具体数据。我可以做这样的事情:

$output = echo $matches[0]

但是之后?我应该如何使用 $output ?例如,如果我需要第二个单词而不是整行。

现在,在得到答案后,我将文件更新为如下所示:请看看我是否有问题,因为现在无法正常工作。

<!DOCTYPE html>
<html>
  <head>
    <meta content="text/html; charset=windows-1252" http-equiv="content-type">
    <title>my page</title>
  </head>
  <body>
    <div style="text-align: center;">
      <title>My Page</title>
      <form name="myform" action="systemmonitor.php" method="GET">
        <div align="center">My page<br>
          <br>
          <input name="sea1" placeholder="customtext" size="25" type="text"> <br>
          <br>
          <input name="sea2" placeholder="customtext" size="25" type="text"> <br>
          <br>
          <input name="sea" value="submit" type="submit"><br>
          <br>
<?php
$searchthis = $_GET["sea1"];
$matches = array();

$handle = @fopen("systemlines.csv", "r");
if ($handle)
{
    while (($buffer = fgetcsv($handle)) !== FALSE)
    {
        $check = preg_grep("/$searchthis/",$buffer);
        if(!empty($check)){
            $matches[] = $buffer;
        }
    }
    fclose($handle);
}

//show results:
print_r($matches);?>
  </body>
</html>
<?php echo $matches[0][4]; ?>
4

1 回答 1

1

您需要使用fgetcsv而不是fgets

http://php.net/manual/en/function.fgetcsv.php

UPD

<?php
$searchthis = $_GET["sea1"];
$matches = array();

$handle = @fopen("sources.csv", "r");
if ($handle)
{
    while (($buffer = fgetcsv($handle)) !== FALSE)
    {
        $check = preg_grep("/$searchthis/",$buffer);
        if(!empty($check)){
            $matches[] = $buffer;
        }
    }
    fclose($handle);
}

//show results:
print_r($matches);
于 2013-08-10T20:47:28.623 回答