简体   繁体   English

PHP格式化类似于MySQL数组结果的字符串

[英]PHP Formatting a String Similar to MySQL array results

Below is my string called $output it is derived from and fsockopen and fread. 下面是我的名为$ output的字符串,它是从fsockopen和fread派生的。 The only thing that might be able to be changed is the fread but nothing else before this. 唯一可以更改的是fread,但在此之前没有其他要求。

14336.0 K9IA 13-Aug-2012 1750Z washington, dc (throb) 
14336.0 K9IA 13-Aug-2012 1750Z fond du lac, wi (ssb) 
14336.0 K9IA 13-Aug-2012 1752Z calumet, wi (ssb) 
14336.0 K9IA 13-Aug-2012 1752Z carsen city, nv (ssb) 
14336.0 K9IA 13-Aug-2012 1753Z carson city, nv (ssb) 
14336.0 K9IA 13-Aug-2012 1754Z dane, wi (ssb) 
14336.0 K9IA 13-Aug-2012 1759Z dane, wi (cw) 
14336.0 KA2TED 13-Aug-2012 1759Z Carson City,NV(SSB) 
14336.0 K9IA 13-Aug-2012 1800Z dane, wi (psk) 
14336.0 K9IA 13-Aug-2012 1801Z bristol, va (psk) 
14336.0 K9IA 13-Aug-2012 1815Z caeson city, nv (rtty) 
14336.0 K9IA 13-Aug-2012 1816Z carson city, nv (rtty)

I then take the string $output and do the following: 然后,我使用字符串$ output并执行以下操作:

$output = str_replace("\n", "<br>", $output);

This inserts a line feed after the closing bracket ) and forms 12 lines. 这将在换行符()后面插入换行符,并形成12行。 Perfect so far. 到目前为止完美。

What I need to do is take the $output now and be able to display it in a nicely formatted table. 我需要做的是现在获取$ output并能够在格式正确的表中显示它。

What I mean is...... 我的意思是......

Each is a field, as in an array.... I would like to use the same format as if it was an MySql data as in: 每个都是字段,就像在数组中一样。...我想使用与MySql数据相同的格式,如下所示:

    while($row = mysqli_fetch_array($result))
    echo $row['Freq'];
    echo "</td><td>";
    echo $row['Call'];
    echo "</td><td>";
    echo $row['Date'];
    echo "</td><td>"; 
    echo $row['Time'];
    echo "</td><td>";
    echo $row['CTYState'];
    echo "</td><td>";
    echo $row['Mode'];
    echo "</td><td>";

Reason for each to be separate is I need to preform other functions such as links etc with a given field. 两者分开的原因是我需要在给定的字段上执行其他功能,例如链接等。 I have searched, tried, frustrated and know there must be a way. 我经过搜索,尝试,沮丧并知道必须有一种方法。 I have done this over and over again in VB and PHP either using MySQL or odbc_connect but never with a string. 我已经在VB和PHP中使用MySQL或odbc_connect一遍又一遍地进行了此操作,但从未使用过字符串。

UPDATE.... 更新...。

I use the method Ed Manot posted because I will be able to use the fields for links, different colors etc........ 我使用Ed Manot发布的方法,因为我将能够使用字段进行链接,使用不同的颜色等........

BUT.......... 但..........

It does not really work well. 它确实不能很好地工作。 I can only see the first 2 fields. 我只能看到前2个字段。 The fields I can see are Field 1 and Field 3 only. 我可以看到的字段仅是字段1和字段3。 Using your original code I can only see 1 14336.0 and nothing else. 使用您的原始代码,我只能看到1 14336.0,而没有其他任何东西。 Any ideas? 有任何想法吗?

echo "<table border='1'>";
echo"<tr><th>FieldA</th><th>FiledB</th><th>FiledC</th><th>FieldD</th><th>FieldE</th>    <th>FiledF</th><th>FiledG</th></tr>\n";
//split the output into lines based on the line break character
$lines = explode("\n", $output);
foreach($lines as $line) {
//split the line into fields based on the space character
 $fields = explode(" ", $line);
 echo "<td>" .$fields[0]. "</td>";
 echo "<td>" .$fields[1]. "</td>";
 echo "<td>" .$fields[2]. "</td>";
 echo "<td>" .$fields[3]. "</td>";
 echo "<td>" .$fields[4]. "</td>";
 echo "<td>" .$fields[5]. "</td>";
 echo "<td>" .$fields[6]. "</td>";
 echo "<td>" .$fields[7]. "</td>";

}
echo '</table>';

You need to split your data into arrays: 您需要将数据拆分为数组:

//split the output into lines based on the line break character
$lines = explode("\n", $output);
foreach($lines as $line) {
  //split the line into fields based on the space character
  $fields = explode(" ", $line);
  echo "<td>" . $fields[0] . "</td>";
  echo "<td>" . $fields[1] . "</td>";
  echo "<td>" . $fields[2] . "</td>";
  // etc...
}

http://php.net/manual/en/function.explode.php http://php.net/manual/zh/function.explode.php

Do not replace the carriage return (if really there?) and do something (dirty) like: 不要更换回车架(如果确实在那儿?),并做一些(肮脏的)事情,例如:

$lines = explode("\n", $output);
echo '<table>';
foreach($lines as $line) {
    echo '<tr>';
    $parts = explode(" ", $line);
    echo '<td>'.implode("</td><td>", $parts).'</td>'; 
    echo '</tr>';
}
echo '</table>';

:D :D

I figured out the problem. 我解决了这个问题。 Instead of spaces, carrots ^ were used. 代替空格,使用了胡萝卜^。 They showed up as spaces but were not. 它们显示为空格,但不是。 Used Regex to replace ^ with " ". 使用正则表达式将^替换为“”。 Works now. 现在可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM