PHP中UBB的使用

添加时间:13-08-09 所属分类:PHP工具与代码
  转载自http://www.60ie.net/article/6/507.html

在一些小型论坛或者是小型的评论系统中,经常会遇到“支持UBB”之类的术语,其实UBB代码是HTML的一个变种,通过程序自定义我们的标签,比如“[a]PHP中UBB的使用[/a]”这样的标签,其实质就是利用技术查找[a][/a]标签,将其替换成<a></a>的标准html,说白了,就是将 标准的 html 标记通过技术手段使其简化,其输出出来的结果还是标准的 html。

明白了 ubb 的原理,那么再制作一个简单的 ubb 编辑器就不难了,和 fck 之类的编辑器比较起来,ubb 代码最大的优点就是代码简单,功能很少,简单的ubb只需要一个文件,而且 ubb 标签可以自己来定义,更改起来很方便,在 php 中就是利用替换函数就可以将 html 进行标签化,输出时进行标签的转化,下面是一个 php 中 UBB 使用的源码,仅一个文件,就实现了 ubb 编辑器,网络上有许多变种的 UBB 代码,核心的原理基本上都是一样的。

小说明:[a]http://www.60ie.net[/a] 实际标准的html为 <a href=http://www.60ie.net >http://www.60ie.net</a>,UBB 编辑器将 <a href=http://www.60ie.net >http://www.60ie.net</a> 进行了标签化,也就是 [a]http://www.60ie.net[/a],通过 ubb 标签,代码是不是简洁了许多。

PHP简单UBB界面预览:

[code]<?php
/*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
function get_ubb($str)
{
//[a][/a]超链接
$str=preg_replace("/\[a\](.*)\[\/a\]/i","<a href=\\1 >\\1</a>",$str);
//QQ号码UBB
$str = preg_replace("/\[qq\]([0-9]*)\[\/qq\]/i", "<a target=\"_blank\" href=\"tencent://message/?uin=\${1}&site=www.60ie.net&menu=yes\"><img src=\"http://wpa.qq.com/pa?p=1:\${1}:8\" alt=\"QQ\${1}\" height=\"16\" border=\"0\" align=\"top\" /></a>", $str);
//[]超链接
$str=preg_replace("/\[img\](.*?)\[\/img\]/i","<img src=\\1 />",$str);
return $str;
}
if($_POST['sub'])
{
echo $ss=get_ubb($_POST['text']);
}
?>
<script>
function inserttag(topen,tclose){
var themess = document.getElementById('test');//编辑对象
themess.focus();
if (document.selection) {//如果是否ie浏览器
var theSelection = document.selection.createRange().text;//获取选区文字
//alert(theSelection);
if(theSelection){
document.selection.createRange().text = theSelection = topen+theSelection+tclose;//替换
}else{
document.selection.createRange().text = topen+tclose;
}
theSelection='';
}else{//其他浏览器
var scrollPos = themess.scrollTop;
var selLength = themess.textLength;
var selStart = themess.selectionStart;//选区起始点索引,未选择为0
var selEnd = themess.selectionEnd;//选区终点点索引
if (selEnd <= 2)
selEnd = selLength;
var s1 = (themess.value).substring(0,selStart);//截取起始点前部分字符
var s2 = (themess.value).substring(selStart, selEnd)//截取选择部分字符
var s3 = (themess.value).substring(selEnd, selLength);//截取终点后部分字符
themess.value = s1 + topen + s2 + tclose + s3;//替换
themess.focus();
themess.selectionStart = newStart;
themess.selectionEnd = newStart;
themess.scrollTop = scrollPos;
return;
}
}
</script><style type="text/css">
<!--
a {
font-size: 12px;
}
-->
</style>
<hr/>
<form action="" method="post">
<a href="javascript:void(0);" onclick='inserttag("[a]","[/a]");'>添加A标签</a>
<a href="javascript:void(0);" onclick='inserttag("[qq]","[/qq]");'>添加QQ标签</a>
<a href="javascript:void(0);" onclick='inserttag("[img]","[/img]");'>添加IMG标签</a><br/>
<textarea id="test" name="text" rows="10" cols="50" wrap="off"></textarea><br/>
<input type="submit" name="sub" value="提交"/>
</form>[/code]

前篇:美国4岁男童连任旅游小镇镇长 日常工作包括... 后篇:install.php简单制作
发表我的评论


推荐文章   phpBB官方支持站 (英文和中文繁简体)   PHP中UBB的使用         
随机文章   php完全过滤HTML,JS,CSS等标签   UBB标签转html代码的PHP函数   PHP实现HTML代码转UBB标签   php截取指定字符之间内容的类与实例(含...   国外网页设计网站建设论坛
广告

其他推荐

因为本站所有内容均转载自其它媒体,本意为公众提供免费服务,但并不代表本网赞同其观点,也不能对其真实性验证负责,如稿件版权单位或个人不想在本网发布,请与我联系,本人会立即将其撤除,谢谢.联系方式:atseashawk@163.com QQ:99289555