自定义ubb代码,preg_replace()函数的一些代码

所属分类: 网络编程 / 正则表达式 阅读数: 2099
收藏 0 赞 0 分享
function ubb($Text) { 
  $Text=htmlspecialchars($Text); 
  $Text=ereg_replace("\r\n","<br>",$Text); 
  $Text=ereg_replace("\r","<br>",$Text); 
  $Text=nl2br($Text); 
  $Text=preg_replace("/\\t/is","  ",$Text); 
  $Text=preg_replace("/\[h1\](.+?)\[\/h1\]/is","<h1>\\1</h1>",$Text); 
  $Text=preg_replace("/\[h2\](.+?)\[\/h2\]/is","<h2>\\1</h2>",$Text); 
  $Text=preg_replace("/\[h3\](.+?)\[\/h3\]/is","<h3>\\1</h3>",$Text); 
  $Text=preg_replace("/\[h4\](.+?)\[\/h4\]/is","<h4>\\1</h4>",$Text); 
  $Text=preg_replace("/\[h5\](.+?)\[\/h5\]/is","<h5>\\1</h5>",$Text); 
  $Text=preg_replace("/\[h6\](.+?)\[\/h6\]/is","<h6>\\1</h6>",$Text); 

  $Text=preg_replace("/\[url\](http:\/\/.+?)\[\/url\]/is","<a href=\\1>\\1</a>",$Text); 
  $Text=preg_replace("/\[url\](.+?)\[\/url\]/is","<a href=\"http://\\1\">http://\\1</a>",$Text); 
  $Text=preg_replace("/\[url=(http:\/\/.+?)\](.*)\[\/url\]/is","<a href=\\1>\\2</a>",$Text); 
  $Text=preg_replace("/\[url=(.+?)\](.*)\[\/url\]/is","<a href=http://\\1>\\2</a>",$Text); 

  $Text=preg_replace("/\[img\](.+?)\[\/img\]/is","<img src=\\1>",$Text); 
  $Text=preg_replace("/\[color=(.+?)\](.+?)\[\/color\]/is","<font color=\\1>\\2</font>",$Text); 
  $Text=preg_replace("/\[size=(.+?)\](.+?)\[\/size\]/is","<font size=\\1>\\2</font>",$Text); 
  $Text=preg_replace("/\[sup\](.+?)\[\/sup\]/is","<sup>\\1</sup>",$Text); 
  $Text=preg_replace("/\[sub\](.+?)\[\/sub\]/is","<sub>\\1</sub>",$Text); 
  $Text=preg_replace("/\[pre\](.+?)\[\/pre\]/is","<pre>\\1</pre>",$Text); 
  $Text=preg_replace("/\[email\](.+?)\[\/email\]/is","<a href=\\1>\\1</a>",$Text); 
  $Text=preg_replace("/\[i\](.+?)\[\/i\]/is","<i>\\1</i>",$Text); 
  $Text=preg_replace("/\[b\](.+?)\[\/b\]/is","<b>\\1</b>",$Text); 
  $Text=preg_replace("/\[quote\](.+?)\[\/quote\]/is","<blockquote><font size='2' face='Courier New'>quote:</font><hr>\\1<hr></blockquote>", $Text); 
  $Text=preg_replace("/\[code\](.+?)\[\/code\]/is","<blockquote><font size='2' face='Courier New'>code:</font><hr color='lightblue'><i>\\1</i><hr color='lightblue'></blockquote>", $Text); 
  $Text=preg_replace("/\[sig\](.+?)\[\/sig\]/is","<div style='text-align: left; color: darkgreen; margin-left: 5%'><br><br>--------------------------<br>\\1<br>--------------------------</div>", $Text); 
return $Text; 

function str($msg){
  global $admin;
  if(!$admin[html]) $msg=htmlspecialchars($msg);
  if($admin[ubb]) $msg=ubb($msg);
  $msg=nl2br($msg); #处理message
  $msg= str_replace("\n","",$msg); #处理message
  $msg= str_replace("\r","",$msg); #处理message
  return $msg;
}
function error($msg){
global $admin;
?>

中的
$Text=preg_replace("/\[h2\](.+?)\[\/h2\]/is","<h2>\\1</h2>",$Text); 

里面的 ("/\[h2](.+?)\[\/h2]/is","<h2>\\1</h2>",$text)

/\[h2](.+?)\[\/h2]/is  转化成 <h2>\\1</h2>

/\[h2](.+?)\[\/h2]/is  里面的 这些"/\(.+?)\ "之类的符号是怎么看的?是什么意思?
______________________________________________________________________________________________

\是转意,\本身是特殊符号,如果要作为普通字符就要用\来转意,就是说\\表示()也是特殊符号,所以要用\转意
更多精彩内容其他人还在看

正则表达式匹配 非XXX的行

问题:如何匹配"非:.+123.123.123.10.+ " 行
收藏 0 赞 0 分享

正则表达式不包含属性

一个标签里不包含某个属性 的 正则表达式的写法
收藏 0 赞 0 分享

ASP正则函数替换分页后的参数

在分页系统里面用到的把page后面得东西都给丢掉
收藏 0 赞 0 分享

asp match正则函数使用Matchs实例

asp matchs函数提供了对正则表达式匹配的只读属性的访问。一直都用这个函数,没想到本站竟然没有这类文章,汗一个,最近我会多加一些这样的文章
收藏 0 赞 0 分享

asp 图片正则 替换,替换前检查图片是不是本地地址的方法

这个图片正则先检查图片的地址,不是本地的则用本地的asp突破盗链,方便使用,注意是答chinaz的朋友问的一个问题
收藏 0 赞 0 分享

java正则表达式彻底研究

从J2SE1.4起Java增加了对正则表达式的支持就是java.util.regex包
收藏 0 赞 0 分享

正则表达式口诀 正则表达式学习工具

正则表达式口诀 + 常用的正则表达式 + 正则表达式学习工具+正则处理工具 正则是每个程序员绕不开的堡垒,只有把它攻下来。我觉得正则之所以难,第一难是需要记忆,第二难是要求具备抽象逻辑思维。
收藏 0 赞 0 分享

比较实用的正则表达式学习笔记

最近在学习正则,一些比较有用的东西怕忘记,记下来,比较乱,想一条记录一条
收藏 0 赞 0 分享

asp只采集网站可见文本的正则

它可以过虑Js 可以过滤 CSS 过滤HTML标识,只采集页面的可见文本。
收藏 0 赞 0 分享

asp.net常用正则表达式

比较常用的多种语言支持的正则整理收集
收藏 0 赞 0 分享
查看更多