dongyi1111 2013-08-10 20:38
浏览 55

在数组中搜索系列(PHP)

I have php output like this:

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)

And this php code that generate this data.

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

Now I want to show a specific line on a specific column somewhere in the page and if I use:

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

it will show me the whole line, and not the specific data that i want. i can make something like this:

$output = echo $matches[0]

but then? how I should use the $output ?? for example if i need the second word instead of or the whole line.

now, after the the answer i update my files to looks like this: pls see if i am somewhere wrong, becouse is now not working.

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

1条回答 默认 最新

  • dongxiaoshe0737 2013-08-10 20:47
    关注

    You need to use fgetcsv instead of 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);
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图