[英]Converting text to html table using PHP
我将使用空格分隔的文本转换为html表。 这是我正在使用的代码,但是输出中不包含“ |” 我可以尝试将其用作选项卡的替代品。
function tabs_to_table($input) {
define('TAB_REPLACEMENT', "|");
define('NEWLINE_REPLACEMENT', "\n");
define('ROW_BEGIN', "");
define('TABLE_BEGIN', "\n");
define('TABLE_END', "\n");
$input = preg_replace("/\t/", TAB_REPLACEMENT , $input);
$rows = preg_split("/\r\n/", $input);
$output = "";
foreach ($rows as $index => $row) {
if (strlen($row) > 0) {
$output .= ROW_BEGIN . $row . NEWLINE_REPLACEMENT;
}
}
//build table
$input = TABLE_BEGIN . $output . TABLE_END;
return $input;
}
输出是
aggr0 367GB 350GB 17GB 95%
aggr0/.snapshot 0TB 0TB 0TB 0%
aggr1 1102GB 554GB 547GB 50%
aggr1/.snapshot 0TB 0TB 0TB 0%
但我想要
aggr0 |367GB |350GB|17GB |95%
aggr0/.snapshot|0TB |0TB |0TB |0%
aggr1 |1102GB|554GB|547GB|50%
aggr1/.snapshot|0TB |0TB |0TB |0%
有人可以指出我做错了什么吗? 谢谢。
为此,您需要str_pad()函数,有关更多信息, 请参见此处: http : //www.w3schools.com/php/func_string_str_pad.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.