Nov 19

类TeX的UBB表格函数 不指定

felix021 @ 2009-11-19 02:29 [IT » 程序设计] 评论(0) , 引用(0) , 阅读(4083) | Via 本站原创 | |
引用
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]

效果:
左对齐 右对齐 左对齐 <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("&#124;", "|", $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
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   *非必须
网址   电邮   [注册]