Home php教程 php手册 PHP操作ubb代码类

PHP操作ubb代码类

Jun 21, 2016 am 09:05 AM
gt lt nbsp this

ubb

//ubbcode类  
class ubbcode
{  
    var $nest;  // 递归深度,for debug
    //可处理标签及处理函数表  
    var $tags = array(
        'url' => '$this->url',  
        'email' => '$this->email',  
        'mail' => '$this->email',  // 为了容错,[mail]和等效
        'img' => '$this->img',  
        'b' => '$this->simple',  
        'i' => '$this->simple',  
        'u' => '$this->simple',  
        'tt' => '$this->simple',  
        's' => '$this->simple',  
        'strike' => '$this->simple',  
        'h1' => '$this->simple',  
        'h2' => '$this->simple',  
        'h3' => '$this->simple',  
        'h4' => '$this->simple',  
        'h5' => '$this->simple',  
        'h6' => '$this->simple',  
        'sup' => '$this->simple',  
        'sub' => '$this->simple',  
        'em' => '$this->simple',  
        'strong' => '$this->simple',  
        'code' => '$this->simple',    
        'small' => '$this->simple',  
        'big' => '$this->simple',  
        'blink' => '$this->simple',
        'fly' => '$this->fly',
        'move' => '$this->move',
        'glow' => '$this->CSSStyle',
        'shadow' => '$this->CSSStyle',
        'blur' => '$this->CSSStyle',
        'wave' => '$this->CSSStyle',
        'sub' => '$this->simple',
        'sup' => '$this->simple',
        'size' => '$this->size',
        'face' => '$this->face',
        'font' => '$this->face',  // 为了容错,[font]和[face]等效
        'color' => '$this->color',
        'html' => '$this->html',
        'quote' => '$this->quote',
        'swf' => '$this->swf',
        'upload' => '$this->upload'
        );  
    function ubbcode()
    {  
      $this->$nest= 0;
      $this->$sLastModified= sprintf("%s", date("Y-m-j H:i", getlastmod()));
    }  
 
    /***********************************************************************
    *  对使用者输入的 E-Mail 作简单的检查,
    *  检查使用者的 E-Mail 字串是否有 @ 字元,
    *  在 @ 字元前有英文字母或数字,在之后有数节字串,
    *  最后的小数点后只能有二个或三个英文字母。
    *  super@mail.wilson.gs 就可以通过检查,super@mail.wilson 就不能通过检查
    ************************************************************************/
    function emailcheck($str) 
    {
      if (eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $str)) 
        return true;
      else
        return false;  
    }
    /***********************************************************************
    *  对使用者输入的 URL 作简单的检查,
    *  目前只能简单判断,不能自动检查fpt,finger等
    ************************************************************************/
    function checkURL($str) 
    {
      $bValidURL= true;
      if (eregi("([a-z0-9-]+([\.][a-z0-9\-]+)+)", $str, $er_arr)) 
      {    
  /*
  printf ("0. %s
\n", $er_arr[0]);
  printf ("1. %s
\n", $er_arr[1]);
  printf ("2. %s
\n", $er_arr[2]);
  printf ("3. %s
\n", $er_arr[3]);
  printf ("4. %s
\n", $er_arr[4]);
  */
      }
      else
         $bValidURL= false;
      return $bValidURL;
    }
    /***********************************************************************
    *  对使用者输入的 图片URL 作简单的检查,
    *  目前只能简单判断结尾是否为图片文件
    *  不支持由CGI动态生成的图片,比如计数器这类的
    ************************************************************************/
    function checkImgURL($str) 
    {
      if ($this->checkURL($str)) {
        if(eregi("\.(jpeg|jpg|gif|bmp|png|pcx|tiff|tga|lwf)$", $str)) 
          return true;
        else
          return false;
      }
      else
        return false;
    }
    /***********************************************************************
    *  自动补全URL部分,主要是协议前缀,
    *  默认是htpp://,支持https://;ftp://;finger://;gopher://等
    *  函数并不对URL的合法性作检查
    ************************************************************************/
    function formatURL($str) 
    {
      if (!eregi("^(ftp|http|https|mms|gopher|finger|bbs|telnet):(\/\/|\\\\)", $str))
        $str= 'http://'.$str;
      return $str;
    }
    //对$str进行UBB编码解析  
    function parse($str)
    {  
        $nest ++;
        $parse = ''.($str);  
        $ret = '';  
        while(true){  
            //查找[xx] 或者[xx=xx] , 但不包括[xx=]
            $eregi_ret=eregi("\[([a-z][a-z0-9]{0,7})(=[a-zA-Z0-9#.:/&@|\?,%=_\+\"\']+)?\]", $parse, $eregi_arr); 
            if(!$eregi_ret)
            {  
                $ret .= $parse;  
                break; //如果没有,返回  
            }
/*  for Debug
            else 
            {
              printf ("$. %s
", $eregi_ret);
              printf ("0. %s
", $eregi_arr[0]);
              printf ("1. %s
", $eregi_arr[1]);
              printf ("2. %s
", $eregi_arr[2]);
              printf ("3. %s
", $eregi_arr[3]);
            }
*/
 
            $pos = @strpos($parse, $eregi_arr[0]);  // 起始位置
            $tag_start= $eregi_arr[1];
            $tag= strtolower($eregi_arr[1]);
            $tag_param= $eregi_arr[2];
            $parse2 = substr($parse, 0, $pos);//标记之前
            $parse = substr($parse, $pos + $eregi_ret);//标记之后
            if(!isset($this->tags[$tag]))
            {  
                $ret .= $parse2.'['.$tag_start.']';  
                continue;    //如果是不支持的标记  
            }  
            //查找对应的结束标记  
            $eregi_ret=eregi("\[(/".$tag.")\]", $parse, $eregi_arr);  
            if(!$eregi_ret)
            {  
                $ret .= $parse2.'['.$tag_start.$tag_param.']';  
                continue;//没有对应该的结束标记  
            }  
            $pos= strpos($parse, $eregi_arr[0]);  
            $value= substr($parse, 0, $pos);   //起止标记之间的内容
            $tag_end= $eregi_arr[1];
            $parse= substr($parse, $pos + $eregi_ret);//结束标记之后的内容  
            // 允许嵌套标记,递归分析
            if (!(($tag == 'code') or ($tag=="url") or ($tag=="email") or ($tag=="img"))){
                $value= $this->parse($value);  
            }
            $ret.= $parse2;
            $parseFun= sprintf('$ret .= %s($tag_start, $tag_param, $tag_end, $value);', $this->tags[$tag]); 
            eval($parseFun);  
        }  
        $nest --;
        return $ret;  
    }
  /*****************************************************
    * 简单替换,类似[b]变为
    * 标签内容不便,只是替代括号为
    *****************************************************/
    function simple($start, $para, $end, $value){
        if (strlen($para) > 0) 
          return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
        else
          return sprintf("%s", $start, $value, $end);
    }
    /*****************************************************
    * 如下认为合法可以没有“http://”;ftp一定要自己加“ftp://”
    * 93611
    *
    * http://www.fogsun.com
    *****************************************************/
    function url($start, $para, $end, $value){  
        $sA= $value;
        $sURL= substr(trim($para), 1);
        if (strlen($sURL) > 0) 
        {
          if (strlen($value) == 0) 
            $sA= $sURL;
        }
        else 
        {
          $sURL= trim($value);
        }
        $sURL= $this->formatURL($sURL);
        if($this->checkURL($sURL)) 
          return "$sA";  
        else {
          return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
        }
    }  
    /*****************************************************
    * 如下认为合法可以没有“mailto:”头;
    * [email=pazee@21cn.com]pazee
    *
    * pazee@21cn.com
    *****************************************************/
    function email($start, $para, $end, $value){  
        $sA= $value;
        $sURL= substr(trim($para), 1);
        if (strlen($sURL) > 0) 
        {
          if (strlen($value) == 0) 
            $sA= $sURL;
        }
        else 
        {
          $sURL= trim($value);
        }
        //if (strtolower(substr($sURL, 0, 7)) != "mailto:")  
          $sURL= "mail.php?email=". $sURL;  
        if($this->emailcheck(substr($sURL, 15))) 
          return "$sA";  
        else
          return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
    }  
    /*****************************************************
    * 显示图片;如下用法认为合法
    * [img=www.21cn.com/title.jpg][/img]
    *
    *****************************************************/
    function img($start, $para, $end, $value){  
        $sURL= substr(trim($para), 1);
        if (strlen($sURL)           $sURL= trim($value);
        //$sURL= $this->formatURL($sURL);
        if ($this->checkImgURL($sURL))  
          return sprintf("\"从新窗口中浏览\"", $sURL,$sURL);  
        else
          return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
    }  
    /*****************************************************
    * 字符串从右向左循环移动 
    * 无参数
    * 等效与html的
    *****************************************************/
    function fly($start, $para, $end, $value){  
      if (strlen($para)>0) // 有参数
        return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
      else
        return ''.$value.'';  
    }  
    /*****************************************************
    * 字符串来回移动 
    * 无参数
    * 等效与html的
    *****************************************************/
    function move($start, $para, $end, $value) {
      if (strlen($para)>0) // 有参数
        return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
      else
        return ''.$value.'';  
    }  
    /*****************************************************
    * 字符晕光效果包括 glow、shadow和blur
    * 字符晕光效果[glow=a,b,c]或者[shadow=a,b,c]
    * 3个参数允许缺省
    * 实现文字阴影特效,
    * glow, shadow,blur 属性依次为颜色、宽度和边界大小
    * wave 属性依次为变形频率、宽度和边界大小
    *****************************************************/
    function CSSStyle(&$start, &$para, &$end, &$value){
        $rets= sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
        if (strlen($para)==0) 
        {
          $para="=,,";
        }
        if (eregi("^=([#]?[[:xdigit:]]{6}|[a-z0-9]*),([0-9]*),([0-9]*)", $para, $er_arr))
        {
          $color=  ($er_arr[1] != "") ? $er_arr[1] : red;   // Default Color
          $width=  ($er_arr[2] != "") ? $er_arr[2] : 400;   // Default Width
          $border= ($er_arr[3] != "") ? $er_arr[3] : 5;     // Default Border
          switch ($start) 
          {
            case "glow":
            case "shadow":
              $rets= sprintf("%s", $start, $color, $border, $width, $value);
              break;
            case "blur";
              $rets= sprintf("%s", $start, $border, $color, $width, $value);
              break;
            case "wave":
              $color=  ($er_arr[1] != "") ? $er_arr[1] : 4;   // Default Color
              $border= ($er_arr[3] != "") ? $er_arr[3] : 2;     // Default Border
              $rets= sprintf("%s", $start, $color, $border, $width, $value);
              break;
          }
        }
        return  $rets;
    }  
    /*****************************************************
    * 字体颜色 xxx 
    * n 可以是 #xxxxxx 或者 xxxxxx (6位16进制数)
    * red,greed,blue,black等颜色保留字也有效
    * 等效与html的xxx
    * [color]xxxx[/color]等效于 [color=red]
    *****************************************************/
    function color($start, $para, $end, $value){
        $cl= strtolower(substr($para, 1));
        if ($cl == "")
          $cl= "red";
        if (eregi("(^[#]?[[:xdigit:]]{6})|red|green|blue|yellow|blue|white|gray|brown|silver|purple|orange" ,$cl)) 
          return sprintf("%s",$cl, $value);
        else
          return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
    }
    /*****************************************************
    * 字体大小 xxx 1    * 等效与html的xxx
    *****************************************************/
    function size($start, $para, $end, $value){
        $size= substr($para, 1);
        if ($size >=1 && $size 1))
          return sprintf("%s",$size, $value);
        else
          return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
    }  
    /*****************************************************
    * 字体名字 [face=n] n字体名称,不需要引号
    * 等效与html的xxx
    *****************************************************/
    function face($start, $para, $end, $value){
        $fn= substr($para, 1);
        if (!eregi("[[:punct:]]", $fn) && strlen($para) > 1) {
          switch (strtoupper($fn))
          {
            case "ST":
              $fn= "宋体";
              break;
            case "HT":
              $fn= "黑体";
              break;
            case "KT":
              $fn= "楷体_GB2312";
              break;
            case "FT":
              $fn= "仿宋_GB2312";
              break;
            case "YY":
              $fn= "幼圆";
              break;
            case "LS":
              $fn= "隶书";
              break;
            case "XST":
              $fn= "新宋体";
              break;
            default:
              $fn= substr($para, 1);
          }
          return sprintf("%s",$fn, $value);
        }
        else
          return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
    }  
     /*****************************************************
    * 文件上传[upload]
    *****************************************************/
    function upload($start, $para, $end, $value){
        $fn= trim(substr($para, 1));
        if (!eregi("[[:punct:]]", $fn) && strlen($para) > 1) {
            if (eregi("jpg|jpeg|bmp|gif|png", $fn)) {
                if ($this->checkImgURL($value))  
                    return sprintf("PHP操作ubb代码类 此主题相关图片如下:

\"从新窗口中浏览\"
",$fn,$value,$value);
                else 
                    return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);    
            } elseif ($fn == "swf") {
                return sprintf("
PHP操作ubb代码类 此主题相关Flash:


全屏欣赏 (点右键->另存为可将动画下载)
",$fn,$value,$value);
            } elseif (eregi("rar|zip|doc", $fn)) {
                return sprintf("PHP操作ubb代码类 点击下载此主题相关附件
",$fn,$value);
            }
        } else 
            return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);            
    }     
    /*****************************************************
    * 调试代码标签[html]
    *****************************************************/
    function html($start, $para, $end, $value)
 
    {
      if (strlen($value) > 0) {
          $value = eregi_replace('
', "", $value);
          return sprintf("

[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

",$value);
      } else {
          return sprintf("[%s]%s[%s]", $start, $value, $end);
      }
    }
    /*****************************************************
    * 引用标签[quote]
    *****************************************************/
    function quote($start, $para, $end, $value)
    {
      if (strlen($value) > 0) {
          return sprintf("
以下为引用内容:
%s

",$value);
      } else {
          return sprintf("[%s]%s[%s]", $start, $value, $end);
      }
    }
    /*****************************************************
    * FLASH[swf]
    *****************************************************/
    function swf($start, $para, $end, $value)
 
    {
 
      if (strlen($value) > 0) {
 
          return sprintf ("

全屏欣赏 (点右键->另存为可将动画下载)
",$value,$value);
 
      } else {
 
          return sprintf("[%s]%s[%s]", $start, $value, $end);
 
      }
 
    }
 
}  
?>



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

See all articles