0

我有一排这样的行,这些是来自搜索的 php 代码的 html 输出行。

这是浏览器输出页面源(不是服务器源)

**<!DOCTYPE html>
<html>
  <head>
    <meta`enter code here` 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> 
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)
<br>
        </div>
      </form>
    </div>
  </body>
</html>

这是浏览器服务器源(php)

    **<!DOCTYPE html>
    <html>
      <head>
        <meta`enter code here` 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("sources.csv", "r");
if ($handle)
{
    while (!feof($handle))
    {
        $buffer = fgets($handle);
        if(strpos($buffer, $searchthis) !== FALSE)
            $matches[] = $buffer;
    }
    fclose($handle);
}

//show results:
$myString = implode( ', ', $matches );
print_r($myString);
?><br>
    <br>
            </div>
          </form>
        </div>
      </body>
    </html>

现在,在真正的页面中,data0、data1、data2... 是一个 php 代码,它从上面的 html 表单中生成这些东西,现在 php 输出我不按原样看到,但我想转换为变量,并只显示我想要的变量,像这样可以:

var parsed[0][0]=data00;
var parsed[0][1]=data01;
var parsed[0][2]=data02;
var parsed[0][3]=data03;
var parsed[1][0]=data10;


0,X (X 是列号) 1,X (X 是行号)
然后我只显示我想要的数据。例如,我会将这段代码放在页面中的某个位置:

document.write(parsed[0][3]);

data00 data01 只是 php 将从以前生成的自定义数据。

我怎么能做这个转换?

4

1 回答 1

0

这是你想要的吗?

// the string containing the data
var externalData = 'one, two, three\n'
                 + 'four, five, six\n'
                 + 'seven, eight, nine';

// split the string into rows, and split each row into cells
var parsed = externalData.split('\n').map(function(row) {
    return row.split(', ');
});

console.log(parsed[0][1]);  // 'two'
于 2013-08-10T16:54:48.473 回答