Nov
19
引用
UBB内容为(开头那个下划线不需要加):
[_table]
l | r | l | c | r
左对齐|右对齐|左对齐|<a href="javascript:alert(12345)">居中啦啦啦</a>|右对齐
1|23|456|7890[newline]1234|0000
abcd|efg|hi|[-]|j
1|[-]|[-]|1|1
[/table]
[_table]
l | r | l | c | r
左对齐|右对齐|左对齐|<a href="javascript:alert(12345)">居中啦啦啦</a>|右对齐
1|23|456|7890[newline]1234|0000
abcd|efg|hi|[-]|j
1|[-]|[-]|1|1
[/table]
效果:
左对齐 | 右对齐 | 左对齐 | <a href="javascript:alert(12345)">居中啦啦啦</a> | 右对齐 |
1 | 23 | 456 | 7890 1234 |
0000 |
abcd | efg | hi | j | |
1 | 1 | 1 |
PHP代码:
function maketbl($str) {
$str = str_replace("<br/>", "\n", $str);
$str = str_replace("|", "|", $str);
$str = trim($str);
$lines = explode("\n", $str);
$firstline = explode("|", trim($lines[0]));
$conf = array();
foreach($firstline as $v) {
switch($v) {
case 'l':
$conf[] = 'left';
break;
case 'r':
$conf[] = 'right';
break;
case 'c':
$conf[] = 'center';
break;
default:
$conf[] = '';
}
}
$ncols = count($conf);
$nline = count($lines);
$ret = '<table cellspacing="0" cellpadding="3" border="1">'. "\n";
for ($i = 1; $i < $nline; $i++) {
$line = trim($lines[$i]);
if (empty($line)) continue;
$line = explode("|", $line);
$ret .= "<tr>\n";
$next = 1;
for ($j = 0; $j < $ncols; $j++) {
if ($line[$j] == "[-]") {
$next++;
continue;
}
$td = $line[$j];
//$td = htmlspecialchars($td);
$td = str_replace("[newline]", "<br/>", $td);
$ret .= <<<eot
<td align="{$conf[$j]}" colspan="{$next}">{$td}</td>
eot;
if ($line[$j] != "[-]") {
$next = 1;
}
}
$ret .= "</tr>\n";
}
$ret .= "</table>";
return $ret;
}
$str = str_replace("<br/>", "\n", $str);
$str = str_replace("|", "|", $str);
$str = trim($str);
$lines = explode("\n", $str);
$firstline = explode("|", trim($lines[0]));
$conf = array();
foreach($firstline as $v) {
switch($v) {
case 'l':
$conf[] = 'left';
break;
case 'r':
$conf[] = 'right';
break;
case 'c':
$conf[] = 'center';
break;
default:
$conf[] = '';
}
}
$ncols = count($conf);
$nline = count($lines);
$ret = '<table cellspacing="0" cellpadding="3" border="1">'. "\n";
for ($i = 1; $i < $nline; $i++) {
$line = trim($lines[$i]);
if (empty($line)) continue;
$line = explode("|", $line);
$ret .= "<tr>\n";
$next = 1;
for ($j = 0; $j < $ncols; $j++) {
if ($line[$j] == "[-]") {
$next++;
continue;
}
$td = $line[$j];
//$td = htmlspecialchars($td);
$td = str_replace("[newline]", "<br/>", $td);
$ret .= <<<eot
<td align="{$conf[$j]}" colspan="{$next}">{$td}</td>
eot;
if ($line[$j] != "[-]") {
$next = 1;
}
}
$ret .= "</tr>\n";
}
$ret .= "</table>";
return $ret;
}
欢迎扫码关注:
转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php 。