簡體   English   中英

PHP格式化類似於MySQL數組結果的字符串

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

下面是我的名為$ output的字符串,它是從fsockopen和fread派生的。 唯一可以更改的是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)

然后,我使用字符串$ output並執行以下操作:

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

這將在換行符()后面插入換行符,並形成12行。 到目前為止完美。

我需要做的是現在獲取$ output並能夠在格式正確的表中顯示它。

我的意思是......

每個都是字段,就像在數組中一樣。...我想使用與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>";

兩者分開的原因是我需要在給定的字段上執行其他功能,例如鏈接等。 我經過搜索,嘗試,沮喪並知道必須有一種方法。 我已經在VB和PHP中使用MySQL或odbc_connect一遍又一遍地進行了此操作,但從未使用過字符串。

更新...。

我使用Ed Manot發布的方法,因為我將能夠使用字段進行鏈接,使用不同的顏色等........

但..........

它確實不能很好地工作。 我只能看到前2個字段。 我可以看到的字段僅是字段1和字段3。 使用您的原始代碼,我只能看到1 14336.0,而沒有其他任何東西。 有任何想法嗎?

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>';

您需要將數據拆分為數組:

//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/zh/function.explode.php

不要更換回車架(如果確實在那兒?),並做一些(骯臟的)事情,例如:

$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

我解決了這個問題。 代替空格,使用了胡蘿卜^。 它們顯示為空格,但不是。 使用正則表達式將^替換為“”。 現在可以使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM