简体   繁体   中英

PHP Formatting a String Similar to MySQL array results

Below is my string called $output it is derived from and fsockopen and fread. The only thing that might be able to be changed is the fread but nothing else before this.

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 = str_replace("\n", "<br>", $output);

This inserts a line feed after the closing bracket ) and forms 12 lines. Perfect so far.

What I need to do is take the $output now and be able to display it in a nicely formatted table.

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:

    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.

UPDATE....

I use the method Ed Manot posted because I will be able to use the fields for links, different colors etc........

BUT..........

It does not really work well. I can only see the first 2 fields. The fields I can see are Field 1 and Field 3 only. Using your original code I can only see 1 14336.0 and nothing else. 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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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