'How can I convert multi text string to table
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
Solution 1:[1]
<?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>";
}
?>
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Archit Gargi |
