Home > Blockchain >  How can I convert multi text string to table
How can I convert multi text string to table

Time:03-11

I have a string that contains multiple lines. how can I convert it into tables where in each td value is every word of each line.

example of the string.

Eth1/1 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T

Eth1/2 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T

expected table outcome:

Eth1/1 | VPC_PEER_KEEPALIVE | connected | routed | full | 1000 | 1000base-T

Eth1/2 | VPC_PEER_KEEPALIVE | connected | routed | full | 1000 | 1000base-T

CodePudding user response:

<?php 
    $str = "Eth1/1 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T
Eth1/2 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T";
// echo $str;

$v1 = explode(PHP_EOL, $str);
foreach ($v1 as $key => $value) {
    $v2 = explode(" ", $value);
    echo "<tr>";
        foreach ($v2 as $key2 => $value2) {
            echo "<td>" .$value2 ."</td>";
        }
    echo "</tr>";
}
?>

CodePudding user response:

This JS code will work-

var string = "Eth1/1 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T"; //Whatever your string is
string = string.split(" ");
var table = "<table id='table' border='1px'><tr>";
for (i = 0; i < string.length; i  ) {
  table  = "<td>"   string[i]   "</td>";
}
table  = "</tr></table>";
document.write(table); //Whatever you want do with your table

  • Related