使用PHP将文本转换为html表
我正在使用将空格分隔的文本转换为html表格。 这是我正在使用的代码,但输出不包含我可以尝试用作选项卡替换的“|”使用PHP将文本转换为html表,php,html,Php,Html,我正在使用将空格分隔的文本转换为html表格。 这是我正在使用的代码,但输出不包含我可以尝试用作选项卡替换的“|” function tabs_to_table($input) { define('TAB_REPLACEMENT', "|"); define('NEWLINE_REPLACEMENT', "\n");
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()函数来完成此操作,请查看此处以了解更多信息:
我认为使用html表格更好。。。因为您的行可能具有动态长度