(备忘)自己写的UBB2HTML与HTML2UBB函数(2 位领导批示)
- 2008-11-11
- 分类:PHP&MySql
- 作者:银子
- 1,270 位领导视察
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function ubb2html($str) { $str = htmlspecialchars(stripslashes($str)); // 字体颜色 $str = preg_replace("#\[color\=([^\]]*)\]([^\[]*)#i","<span style='color:$1'>$2",$str); $str = preg_replace("#\[\/color\]#i","</span>",$str); // 字体大小 $str = preg_replace("#\[size\=(\d)\]([^\[]*)#i","<font size='$1'>$2",$str); $str = preg_replace("#\[\/size\]#i","</font>",$str); // 超链接 $str = preg_replace("#\[url\=([^\]]*)\]([^\[]*)#i","<a href='$1' target='_blank'>$2",$str); $str = preg_replace("#\[\/url\]#i","</a>",$str); // 图片 $str = preg_replace("#\[img\]([^\[]*)\[\/img\]#i","<img src='$1' />",$str); // 其它 $str = preg_replace("#\[([\/]?)b\]#i","<$1strong>",$str); $str = preg_replace("#\[([\/]?)i\]#i","<$1i>",$str); $str = preg_replace("#\[([\/]?)em\]#i","<$1em>",$str); $str = preg_replace("#\[([\/]?)u\]#i","<$1u>",$str); return nl2br($str); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function html2ubb($str) { $str = str_replace("& nbsp;",' ',$str); $str = str_replace("& mdash;",'—',$str); $str = str_replace("& quot;",'“',$str); $str = str_replace("<br />","\n\r",$str); $str = preg_replace("#\<a[^>]+href=\"([^\"]+)\"[^>]*\>(.*?)<\/a\>#i","[url=$1]$2[/url]",$str); $str = preg_replace("#\<font(.*?)color=\"#([^ >]+)\"(.*?)\>(.*?)<\/font\>#i","[color=$2]$4[/color]",$str); $str = preg_replace("#\<font(.*?)size=\"([^ >]+)\"(.*?)\>(.*?)<\/font\>#i","[size=$2]$4[/size]",$str); $str = preg_replace("#\<div[^>]+align=\"([^\"]+)\"[^>]*\>(.*?)<\/div\>#i","[align=$1]$2[/align]",$str); $str = preg_replace("#\<img[^>]+src=\"([^\"]+)\"[^>]*\>#i","[img]$1[/img]",$str); $str = preg_replace("#\<([\/]?)u\>#i","[$1u]",$str); $str = preg_replace("#\<([\/]?)em\>#i","[$1i]",$str); $str = preg_replace("#\<strong\>([^<]*)\<\/strong\>#i","[b]$1[/b]",$str); $str = preg_replace("#\<b\>([^<]*)\<\/b\>#i","[b]$1[/b]",$str); $str = preg_replace("#\<([\/]?)i\>#i","[$1i]",$str); $str = preg_replace("#<[^>]*?>#i","",$str); $str = addslashes($str); return $str; } |
NOTE:本博内容大部分为原创,转载请注明出处。
永久链接:http://www.zdyi.com/ubb2html_html2ubb_function/154

























@Dadait 当然可以![[face2]](http://www.zdyi.com/wp-includes/images/face/2.gif)
偶借来用用.可不可以哇.