PHP-YII-UBB类仅供参考

本文详细介绍了PHP中利用自定义类实现的UBB代码解析与转换过程,包括UBB到HTML的转换、过滤UBB代码、获取内容摘要、截取内容、UBB转别名、UBB反转别名等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
class LibUbb extends CApplicationComponent {
	private $sign = array('\\', '$', '{', '}');
	private static $instance;
	public static function getInstance() {
		if (is_null(self::$instance)) {
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	/**
	 * UBB转HTML
	 * @param string $str
	 * @return string
	 **/
	public function decode($str) {
		if (empty($str)) {
			return '';
		}
		
		if (is_array($str)) {
			extract($str, EXTR_OVERWRITE);
		}
		
		$rplace = $this->rplace($str);
		$str = str_replace(' ', ' ', $str);
		$str = nl2br($str);
		if (!empty($rplace)) {
			$this->rback($str, $rplace);
		}
		
		
		$siteStatic = defined('SITE_STATIC') ? SITE_STATIC : '';
		$search = array(
			'/\[img\]([^\[\]]+)\[\/img\]/i',
			'/\[emoticon=(\d+)\s*\/\]/i',
			'/\[video\]([^\[\]]+)\[\/video\]/i',
			'/\[audio\]([^\[\]]+)\[\/audio\]/i',
			'/\[file]([^\[\]]+)\[\/file\]/i',
			'/\[link]([^\[\]]+)\[\/link\]/i',
		);
		
		$replace = array(
			'<p class="f_s12 f_col_Gray mb_10" ><a href="\\1" class="lightColor_show" ><img style="max-width:618px;" src="\\1" /></a></p>',
			'<img src="'.$siteStatic.'/images/editor/emoticons/\\1.gif" />',
			//'<embed src="\\1" width="480" height="400" allowscriptaccess="always" allowfullscreen="true" type="application/x-shockwave-flash" flashvars="from=ku6"></embed>',
			'<a href="\\1" target="_blank" οnclick="return UbbUtil.decode.video(this);"><img src="'. $siteStatic .'/images/editor/video.png" /></a>',
			'<embed src="\\1" type="video/x-ms-asf-plugin" width="480" height="400" autostart="false" loop="true" />',
			'<a href="\\1" target="_blank">\\1</a>',
			'<a href="\\1" target="_blank">\\1</a>'
		);
		$str = preg_replace($search, $replace, $str);
		
		return $str;
	}
	
	/**
	 * 过滤UBB代码
	 * @param string $str
	 * @param boolean $e
	 * @return string
	 **/ 
	public function strip($str, $e = true) {
		$str = strip_tags($str);
		$str = preg_replace('/\s*\[([\w-]+)\][^\[\]]+\[\/\\1\]\s*/', '', $str);
		if ($e) {
			$str = preg_replace('/\s*\[[\w-]+=[^\[\]]+\s*\/\]\s*/i', '', $str);
		}
		return $str;
	}
	
	/**
	 * 获取一段内容的摘要
	 * @param string $str
	 * @param integer $len
	 * @param string $etc exp:..
	 * @return string
	 */
	public function desc($str, $len = null, $etc = '') {
		if (empty($str)) {
			return '';
		}
		
		$str = $this->strip($str, false);
		if (!is_null($len)) {
			$str = $this->cutstr($str, $len, $etc);
		}
		$str = $this->decode($str);
		
		return $str;
	}
	
	/**
	 * 截取内容,中文=2; 英文=1
	 * @param string $string
	 * @param integer $length
	 * @param string $etc
	 * @param integer $e 一个UBB的长度
	 * @return string
	 */
	public function cutstr($string, $length, $etc = '', $e = 1) {
		if (empty($string)) {
			return '';
		}
		
		$result = '';
		$rplace = $this->rplace($string);
		$total = strlen($string);
		$i = 0;
		while ($length > 0 && $i < $total) {
			if ($skip = strpos(str_pad(decbin(ord($string[$i])), 8, '0', STR_PAD_LEFT), '0')) {
				$length -= 2;
			} else {
				$skip = 1;
				$j = $i + 2;
				if ($string[$i] == $this->sign[0] && $string[$i+1] == $this->sign[1] && $string[$j] == $this->sign[2]) {
					while ($j < $total) {
						$j++;
						$var = $string[$j];
						if ($var == $this->sign[3]) {
							$length -= $e; 
							$skip = $j - $i + 1;
							break;
						}
						
						if (!is_numeric($var)) {
							break;
						}
					}
				}
				$length--;
			}
			
			$result .= substr($string, $i, $skip);
			$i += $skip;
		}
		
		if ($rplace) {
			$this->rback($string, $rplace);
		}
		
		return $string.($i < $total ? $etc : '');
	}
	
	/**
	 * UBB转别名
	 * @param string $str
	 * @return boolean
	 */
	public function rplace(&$str) {
		if (preg_match_all('/\[([\w-]+)\][^\[\]]+\[\/\\1\]|\[[\w-]+=[^\[\]]+\s*\/\]/', $str, $matches)) {
			$count = 1;
			foreach ($matches[0] as $key => $value) {
				$str = str_replace($value, $this->sign[0].$this->sign[1].$this->sign[2].$key.$this->sign[3], $str, $count);
			}
		}
		return isset($matches) && isset($matches[0]) ? $matches[0] : false;
	}
	
	/**
	 * UBB反转别名
	 * @param unknown $str
	 * @param unknown $matches
	 */
	public function rback(&$str, $matches) {
		if (empty($matches)) {
			return;
		}
		
		foreach ($matches as $key => $value) {
			$str = str_replace($this->sign[0].$this->sign[1].$this->sign[2].$key.$this->sign[3], $value, $str);
		}
	}
	
	/**
	 * 获取内容中的所有图片
	 * @param string $str
	 * @return array|boolean
	 */
	public function findImages($str) {
		if (preg_match_all('/\[img\]([^\]]+)\[\/img\]/i', $str, $matches)) {
			$result = array();
			foreach ($matches[1] as $value) {
				if (empty($value)) {
					continue;
				}
				
				if (!preg_match('/^[\w-]{1,6}:\/\/[\w-]+(\.[\w-]+)+(:\d{1,6})?\/[^<>"\']+/', $value)) {
					continue;
				}
				
				$result[] = strip_tags($value);
			}
			return $result;
		}
		return false;
	}
	
	/**
	 * 获取UBB中Text
	 * @param string $str
	 * @return array
	 */
	public function findAll($str) {
		$result = array();
		if (preg_match_all('/\[([\w-]+)\]([^\[\]]+)\[\/\\1\]/', $str, $matches)) {
			foreach ($matches[2] as $key => $value) {
				$tag = $matches[1][$key];
				if (!isset($result[$tag])) {
					$result[$tag] = array();
				}
				$result[$tag][] = str_replace(array('"', "'", '<', '>', ' '), '', $value);
			}
		}
		return $result;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值