插件39:公告栏代码

<?php // Plug-in 39: BB Code

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$text = <<<_END
This is a test of BB Code<br /><br />
[size=12]Size 12[/size]
[size=20]Size 20[/size]
[size=32]Size 32[/size]<br />
[i]italic[/i]
[color=red][b]bold red[/b][/color]
[u]underline[/u]
[s]strikethrough[/s]<br />
[url]http://google.com[/url]<br />
[url=http://yahoo.com]A titled hyperlink[/url]<br />
[quote]Block quoted text[/quote]
_END;

echo PIPHP_BBCode($text);

function PIPHP_BBCode($string)
{
   // Plug-in 39: BB Code
   //
   // This plug-in recognizes and translates BB Code
   // into its HTML equivalent. The argument required is:
   //
   //    $string: A string containing BB Code

   $from   = array('[b]', '[/b]',  '[i]', '[/i]',
                   '[u]', '[/u]',  '[s]', '[/s]',
                   '[quote]',      '[/quote]',
                   '[code]',       '[/code]',
                   '[img]',        '[/img]',
                   '[/size]',      '[/color]',
                   '[/url]');
   $to     = array('<b>', '</b>',  '<i>', '</i>',
                   '<u>', '</u>',  '<s>', '</s>',
                   '<blockquote>', '</blockquote>',
                   '<pre>',        '</pre>',
                   '<img src="',   '" />',
                   '</span>',      '</font>',
                   '</a>');
   $string = str_replace($from, $to, $string);
   $string = preg_replace("/\[size=([\d]+)\]/",
      "<span style=\"font-size:$1px\">", $string);
   $string = preg_replace("/\[color=([^\]]+)\]/",
      "<font color='$1'>", $string);
   $string = preg_replace("/\[url\]([^\[]*)<\/a>/",
      "<a href='$1'>$1</a>", $string);
   $string = preg_replace("/\[url=([^\]]*)]/",
      "<a href='$1'>", $string);
   return $string;
}

?>

插件说明:

插件接受一个包含BB代码的字符串,把它转换为安全的HTML代码并返回,他接受以下参数:

$string 需要转换的字符串。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值