加入客源網 每天多接2個客戶,做生意就是這麼簡單 | 居家清潔請找 易祺清潔公司 |
論壇標籤bbcode的範例 |
房東:新手 發表時間:2007-01-19 |
現在非常多程式用到 bbcode 現在給大家參考一下 bbcode_class.php php程式碼: function begtoend($htmltag){ return preg_replace('/<($A-Za-z]+)>/','$1>',$htmltag); } function replace_pcre_array($text,$array){ $pattern = array_keys($array); $replace = array_values($array); $text = preg_replace($pattern,$replace,$text); return $text; } class bbcode{ var $tags; var $settings; function bbcode(){ $this->tags = array(); $this->settings = array('enced'=>true); } function get_data($name,$cfa = ''){ if(!array_key_exists($name,$this->tags)) return ''; $data = $this->tags$$name]; if($cfa) $sbc = $cfa; else $sbc = $name; if(!is_array($data)){ $data = preg_replace('/^ALIAS(.+)$/','$1',$data); return $this->get_data($data,$sbc); }else{ $data$'Name'] = $sbc; return $data; } } function change_setting($name,$value){ $this->settings$$name] = $value; } function add_alias($name,$aliasof){ if(!array_key_exists($aliasof,$this->tags) or array_key_exists($name,$this->tags)) return false; $this->tags$$name] = 'ALIAS'.$aliasof; return true; } function onparam($param,$regexarray){ $param = replace_pcre_array($param,$regexarray); if(!$this->settings$'enced']){ $param = htmlentities($param); } return $param; } function export_definition(){ return serialize($this->tags); } function import_definiton($definition,$mode = 'append'){ switch($mode){ case 'append': $array = unserialize($definition); $this->tags = $array + $this->tags; break; case 'prepend': $array = unserialize($definition); $this->tags = $this->tags + $array; break; case 'overwrite': $this->tags = unserialize($definition); break; default: return false; } return true; } function add_tag($params){ if(!is_array($params)) return 'Paramater array not an array.'; if(!array_key_exists('Name',$params) or empty($params$'Name'])) return 'Name parameter is required.'; if(preg_match('/$^A-Za-z]/',$params$'Name'])) return 'Name can only contain letters.'; if(!array_key_exists('HasParam',$params)) $params$'HasParam'] = false; if(!array_key_exists('HtmlBegin',$params)) return 'HtmlBegin paremater not specified!'; if(!array_key_exists('HtmlEnd',$params)){ if(preg_match('/^(<$A-Za-z]>)+$/',$params$'HtmlBegin'])){ $params$'HtmlEnd'] = begtoend($params$'HtmlBegin']); }else{ return 'You didn't specify the HtmlEnd parameter, and your HtmlBegin parameter is too complex to change to an HtmlEnd parameter. Please specify HtmlEnd.'; } } if(!array_key_exists('ParamRegexReplace',$params)) $params$'ParamRegexReplace'] = array(); if(!array_key_exists('ParamRegex',$params)) $params$'ParamRegex'] = '$^]]+'; if(!array_key_exists('HasEnd',$params)) $params$'HasEnd'] = true; if(array_key_exists($params$'Name'],$this->tags)) return 'The name you specified is already in use.'; $this->tags$$params$'Name']] = $params; return ''; } function parse_bbcode($text){ foreach($this->tags as $tagname => $tagdata){ if(!is_array($tagdata)) $tagdata = $this->get_data($tagname); $startfind = "/${$tagdata$'Name']}"; if($tagdata$'HasParam']){ $startfind.= '=('.$tagdata$'ParamRegex'].')'; } $startfind.= ']/'; if($tagdata$'HasEnd']){ $endfind = "$/{$tagdata$'Name']}]"; $starttags = preg_match_all($startfind,$text,$ignore); $endtags = substr_count($text,$endfind); if($endtags < $starttags){ $text.= str_repeat($endfind,$starttags - $endtags); } $text = str_replace($endfind,$tagdata$'HtmlEnd'],$text); } $replace = str_replace(array('%%P%%','%%p%%'),''.$this->onparam('$1',$tagdata$'ParamRegexReplace']).'','''.$tagdata$'HtmlBegin'].'''); $text = preg_replace($startfind.'e',$replace,$text); } return $text; } } ?> index.php require_once('bbcode.php'); $bbcode = new bbcode(); $bbcode->add_tag(array('Name'=>'b','HtmlBegin'=>'','HtmlEnd'=>'')); $bbcode->add_tag(array('Name'=>'i','HtmlBegin'=>'','HtmlEnd'=>'')); $bbcode->add_tag(array('Name'=>'u','HtmlBegin'=>'','HtmlEnd'=>'')); $bbcode->add_tag(array('Name'=>'link','HasParam'=>true,'HtmlBegin'=>'','HtmlEnd'=>'')); $bbcode->add_tag(array('Name'=>'color','HasParam'=>true,'ParamRegex'=>'$A-Za-z0-9#]+','HtmlBegin'=>'','HtmlEnd'=>'','ParamRegexReplace'=>array('/^$A-Fa-f0-9]{6}$/'=>'#$0'))); $bbcode->add_tag(array('Name'=>'email','HasParam'=>true,'HtmlBegin'=>'','HtmlEnd'=>'')); $bbcode->add_tag(array('Name'=>'size','HasParam'=>true,'HtmlBegin'=>'','HtmlEnd'=>'','ParamRegex'=>'$0-9]+')); $bbcode->add_tag(array('Name'=>'bg','HasParam'=>true,'HtmlBegin'=>'','HtmlEnd'=>'','ParamRegex'=>'$A-Za-z0-9#]+')); $bbcode->add_tag(array('Name'=>'s','HtmlBegin'=>'','HtmlEnd'=>'')); $bbcode->add_tag(array('Name'=>'align','HtmlBegin'=>' ','HtmlEnd'=>' ','HasParam'=>true,'ParamRegex'=>'(center|right|left)'));$bbcode->add_alias('url','link'); /* This outputs: Bold text Italic text Underlinex text A link Another link Red text Email me! 20-point text Text with a red background Text with a line through it Centered text */ print $bbcode->parse_bbcode('$b]Bold text$/b] $i]Italic text$/i] $u]Underlinex text$/u] $link=http://www.twbb.org/]A link$/link] $url=http://www.twbb.org/]Another link$/url] $color=red]Red text$/color] [email protected]]Email me!$/email] $size=20]20-point text$/size] $bg=red]Text with a red background$/bg] $s]Text with a line through it$/s] $align=center]Centered text$/align]'); ?> |
|
廣利不動產-新板特區指名度最高、值得您信賴的好房仲 您的托付,廣利用心為您服務 廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲 完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心! |
姓名: | |||
佈告內容: | |||
其他選項: | |||
|