您的位置:首页 > 编程语言

插件40:Pound代码

2011-10-28 11:45 127 查看
<?php // Plug-in 40: Pound Code

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

$string = <<<_END
This is a test of #comicPound Code#-<br /><br />
#2Size 2#-
#4Size 4#-
#6Size 6#-<br />
#iitalic#i-
#red#bbold red#b-#-
#uunderline#u-
#sstrikethrough#s-<br />
_END;

echo PIPHP_PoundCode($string);

function PIPHP_PoundCode($text)
{
// Plug-in 40: Pound Code
//
// This plug-in recognizes and translates Pound Code
// (also known as hash code) into its HTML equivalent.
// Arguments required are:
//
//    $text:    A string containing Pound Code

$names = array('#georgia', '#arial',   '#courier',
'#script',  '#impact',  '#comic',
'#chicago', '#verdana', '#times');
$fonts = array('Georgia',  'Arial',    'Courier New',
'Script',   'Impact',   'Comic Sans MS',
'Chicago',  'Verdana',  'Times New Roman');
$to    = array();

for ($j = 0 ; $j < count($names) ; ++$j)
$to[] = "<font face='$fonts[$j]'>";

$text = str_ireplace($names, $to, $text);

$text = preg_replace('/#([bius])-/i', "</$1>",
$text);
$text = preg_replace('/#([bius])/i',  "<$1>",
$text);
$text = preg_replace('/#([1-7])/',    "<font size='$1'>",
$text);
$text = preg_replace('/#([a-z]+)/i',  "<font color='$1'>",
$text);
$text = str_replace('#-', "</font>", $text);

return $text;
}

?>

插件说明:

插件接受一个包含Pound代码的字符串,把它转化为安全的HTML代码,返回转换后的结果。他需要以下参数:

$text:需要转换的文本。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: