検索
ホームページphp教程php手册改良されたUBBクラス

改良されたUBBクラス

Jun 13, 2016 pm 12:38 PM
php著者改善する親切

/*
転載する場合は作者を明記してください

原著者:何志強
改良:SonyMusic[ sonymusic@163.net ]
ファイル: ubb. php
注: 改善と言われていますが、実際にはコア関数 parse() が完全に書き直されており、考え方も異なります。
ただし、それでも He Zhiqiang の例からインスピレーションを受けており、テスト例や URLCHECK などのいくつかの関数も He Zhiqiang のプログラムに従っています。ありがとう、He Zhiqiang さん。
まだカラー機能はありませんが、追加していきます。
プログラムにバグや不都合がある場合は、メールでお知らせください。
ありがとうございます!
機能の改善:
文字列の UBB エンコーディング。このクラスは現在、次の単純で実用的なエンコーディングのみをサポートしています。
1. URL リンク
[url] http://phpuser. ]
http:// ヘッダーは必要ありません
たとえば、[url]phpuser.com[/url] も使用できます。
2. 電子メールのリンク
[email] sonymusic@163.net [/email]
3. 画像のリンク
[img] http://www.phpchina.com/images/logo .gif [/img]
URL リンクと同様に、前の http はオプションです。
4. テキスト
[b]太字[/b]
[i]斜体[/i]
[u]下線[/u]
[h1] タイトル文字 いいえ. 1[/h1] ... [h6]タイトルワード6[/h6]
[sup][/sup]
[sub][/sub]
[tt][/ tt ]
[s][/s]
[strike][/strike]
[em][/em]
[strong][/strong]
[コード][/ コード]
[samp][/samp]
[kbd][/kbd]
[var][/var]
[dfn][/dfn]
[cite][/ cite ]
[small][/small]
[big][/big]
[blink][/blink]
次の点に注意してください:
1. url、email、img タグ
2. タグ内では TAB キーは使用できませんが、スペースは使用できます。
3. このクラスは、htmlencode、htmlencode4textarea、emailcheck 関数、および urlcheck クラスを呼び出す必要があります。
4. 変更後は、ネストがサポートされますが、url、email、および img タグのネストは許可されません。
技術情報:
究極掲示板
http://www.ultimatebb.com/
UBB コードとは
http://www.scriptkeeper.com/ubb/ubbcode.html
*/

include("urlcheck.php");
include("otherfunc.php"); //これら 2 つのファイルの内容は最後に添付されます。

//ubbcode class
class ubbcode{
var $call_time=0;
//扱えるタグと処理関数の対応表
var $tags = array( //小文字タグ => 対応する処理関数
'url' => '$this->url',
'email' => '$this->'img ' = > '$this->img'、
'b' => '$this->シンプル'、
'i' => '$this->シンプル'、
'u' => '$this->シンプル'、
'tt; '$this->シンプル'、'$this-> 、
'strike' => '$this->simple'、
'h1' => '$this->simple'、
'h2' => >シンプル'、
'h3' => '$this->シンプル'、
'h4' => '$this->'h5' => '$this->simple',
'h6' => '$this->sup' => '$this->simple',
'sub ' => '$this->シンプル',
'em' => '$this->シンプル',
'強い' => 🎜>'コード' => '$this->simple'、
'samp' => '$this->simple'、
'kbd' =>シンプル',
'var' => '$this->simple',
'dfn' => '$this->'simple',
'cite' =>これ->シンプル'、
'小さい' => '$これ->シンプル'、
'大きい' => '$これ->シンプル'、
'ブリンク ' = > '$this->simple'
);
//URL 添付ファイル属性
var $attr_url;
//URL 正当性チェック オブジェクト
var $urlcheck; 🎜>関数 ubbcode($attr_url){
$this->attr_url = ''.$attr_url;
$this->
}

// $str
function parse($str){
$this->call_time;
$parse = ''.htmlencode($str); の UBB エンコード解析ret = '';
while(true){
$eregi_ret=eregi("[[#]{0,1}[[:alnum:]]{1,7}]" ,$parse,$ eregi_arr); //[xx] を検索
if(!$eregi_ret){
$ret .= $parse; //そうでない場合は、
$pos = @strpos ($parse,$eregi_arr[0]);
$tag_len=strlen($eregi_arr[0])-2;//タグの長さ
$tag_start=substr($eregi_arr[0],1, $tag_len);
$tag=strto lower($tag_start);

if((($tag=="url") または ($tag== "img")) and ($this->call_time>1)){
echo $this->call_time."
";
return $parse;//ネストできないマークは直接返します
}

$parse2 = substr($parse,0,$pos);//マークの前
$parse = substr($parse, $pos) $tag_len 2);//タグの後
if(!isset($this->tags[$tag])){
echo "$tag_start
";
$ret . parse2.'['.$tag_start.']';
Continue;//サポートされていないタグの場合
}

//対応する終了タグを検索
$ eregi_ret=eregi ("[/".$tag."]",$parse,$eregi_arr);
if(!$eregi_ret){
$ret .= $parse2.'['.$tag_start. ;
Continue;//対応する終了タグがない場合
}
$pos=strpos($parse,$eregi_arr[0]);
$value=substr($parse ,0, $pos);//これは開始タグと終了タグの間の内容です
$tag_end=substr($parse,$pos 2,$tag_len);
$parse=substr($parse,$pos $ tag_len) 3);//終了タグの後のコンテンツ

if(($tag!="url") and ($tag!="email") and ($tag!="img")){
$value=$this->parse($value);
}

$ret .= $parse2;
eval('$ret .= '.$this-> tags[$tag].'("'.$tag_start.'","'.$tag_end.'","'.$value.'");'); ;call_time--;
return $ret;
}

function simple($start,$end,$value){
return ''
}

関数 url($start,$end,$value){
$trim_value=trim ( $value);
if (strto lower(substr($trim_value,0,7))!="http://")
$trim_value="http://".$trim_value
($this->urlcheck->check($trim_value)) return 'attr_url.'>'.$value.' ;/a>';
else return '['.$start.']'.$value.'[/'.$end.']';
}

関数メール ( $start,$end,$value){
if(emailcheck($value)) return '
'.$value.' a> ;';
else return '['.$start.']'.$value.'[/'.$end.']';
}

function img($start , $end,$value){
$trim_value=trim($value);
if ((strto lower(substr($trim_value,0,7))!="http://") または ($ this ->urlcheck->check($trim_value)))
return '改良されたUBBクラス';
else return '[' 。 $start.']'.$value.'[/'.$end.']'
}
}

//テスト
echo '';
echo 'test'; ';
echo '

';
echo '
';
echo 'echo '';

if(isset($ubb)){
$ubbcode = new ubbcode('target="_blank"'); >echo '
'.$ubbcode->parse($ubb);

echo ''; ;

?>


ファイル urlcheck.php の内容:
//urlcheck.php
クラス urlcheck{
var $regex = array(//プロトコル名 (ここでは小文字で記述する必要があることに注意してください) => 対応する正規表現
'ftp' => '$this->ftpurl',
'file ' = > '$this->fileurl'、
'http' => '$this->httpurl'、
'https' => '$this->httpurl'、
'gopher' => '$this->gopherurl'、
'news' => 'nntp' => ,
'telnet' => '$this->telneturl',
'wais' => '$this->waisurl'

var var $hialpha;
var $safe;
var $punctuation; var $reserved;
var $unreserved; 🎜>var $urlpath;
var $user;
var $alphadigit;ドメインラベル;
var $hostname;
var $login;

//ftp
var $fsegment;
var $fpath;

//file

//http ,https
var $search ;
var $hsegment;
var $httpurl;
var $selector; gtype;
var $gopherurl;

//ニュース
var $group;
var $newsurl; 🎜>//nntp
var $nntpurl;

//telnet
var $telneturl;

//wais
var
var $wtype; ;
var $database;
var $waisindex;
var $waisurl; 🎜>$pos = @strpos ($url,':',1);
if($pos$prot = substr($url, 0,$pos); >if(!isset($this->regex[$prot])) return false;
eval('$regex = '.$this->regex[$prot] .';'); >return ereg('^'.$regex.'$',$url);
}

関数 urlcheck(){
$this->
$this->hialpha = '[A-Z]';
$this->alpha = '('.$this->lowalpha.'|'.$ this->hialpha.') ';
$this->数字 = '[0-9]';
$this->safe = '[$. _-]'; [*()'!,]';
$this->national = '([{}|^~`]|[|])';
$this ->句読点 = '[#%"]';
$this->reserved = '[?;/: @&= ]';
$this->hex = ' ('.$this->数字.'|[a-fA-F])';
$this->escape = '(%'.$this->hex.'{2})';
$this->unreserved = '('.$this->alpha.'|'.$this->digit.'|'.$this->safe.'|'.$this- >追加。')';
$this->uchar = '('.$this->未予約.'|'.$this->escape.')';
$this->xchar = '('.$this->未予約.'|'.$this->予約.'|'.$this->escape.')';
$this->digits = '('.$this->digit.' )';

$this->urlpath = '('.$this->xchar.'*)';
$this->パスワード = '(('.$this->uchar.'|[?;&=]'.')*)';
$this->user = '(('.$this->uchar.'|[?;&=]'.')*)';
$this->ポート = $this->数字;
$this->ホスト番号 = '('.$this->数字.'.'.$this->数字.'.'.$this->数字.'.'.$this- >数字。')';
$this->alphadigital = '('.$this->alpha.'|'.$this->digital.')';
$this->toplabel = '('.$this->alpha.'|('.$this->alpha.'('.$this->alphadigit.'|-)*' .$this->英数字.'))';
$this->domainlabel = '('.$this->alphadigit.'|('.$this->alphadigit.'('.$this->alphadigit.'|-)*' .$this->英数字.'))';
$this->ホスト名 = '(('.$this->ドメインラベル.'.)*'.$this->トッププラベル.')';
$this->host = '('.$this->ホスト名.'|'.$this->ホスト番号.')';
$this->hostport = '('.$this->host.'(:'.$this->port.')?)';
$this->login = '(('.$this->user.'(:'.$this->password.')?@)?'.$this->hostport.' )';

$this->ftptype = '[aidAID]';
$this->fsegment = '(('.$this->uchar.'|[?: @&= ])*)';
$this->fpath = '('.$this->fsegment.'(/'.$this->fsegment.')*)';
$this->ftpurl = '([fF][tT][pP]://'.$this->login.'(/'.$this->fpath.'(;[tT ][yY][pP][eE]='.$this->ftptype.')?)?)';

$this->fileurl = '([fF][iI][lL][eE]://('.$this->host.'|[lL][oO][cC ][aA][lL][hH][oO][sS][tT])?/'.$this->fpath.')';

$this->search = '(('.$this->uchar.'|[;: @&= ])*)';
$this->hsegment = '(('.$this->uchar.'|[;: @&= ])*)';
$this->hpath = '('.$this->hsegment.'(/'.$this->hsegment.')*)';
$this->httpurl = '([hH][tT][tT][pP][sS]?://'.$this->ホストポート。'(/'.$this->; hpath.'([?]'.$this->search.')?)?)';

$this->gopher_string = '('.$this->xchar.'*)';
$this->selector = '('.$this->xchar.'*)';
$this->gtype = $this->xchar;
$this->gopherurl = '([gG][oO][pP][hH][eE][rR]://'.$this->ホストポート.'(/('.$this ->gtype.'('.$this->selector.'( '.$this->search.'( '.$this->gopher_string.')?)?)?)?)?) ';

$this->article = '(('.$this->uchar.'|[;/?:&=]) @'.$this->host.')';
$this->group = '('.$this->alpha.'('.$this->alpha.'|'.$this->digital.'|[-. _] )*)';
$this->grouppart = '([*]|'.$this->group.'|'.$this->article.')';
$this->newsurl = '([nN][eE][wW][sS]:'.$this->grouppart.')';

$this->nntpurl = '([nN][nN][tT][pP]://'.$this->ホストポート。'/'.$this->グループ。 '(/'.$this->数字.')?)';

$this->telneturl = '([tT][eE][lL][nN][eE][tT]://'.$this->login.'/?)' ;

$this->wpath = '('.$this->uchar.'*)';
$this->wtype = '('.$this->uchar.'*)';
$this->database = '('.$this->uchar.'*)';
$this->waisdoc = '([wW][aA][iI][sS]://'.$this->ホストポート.'/'.$this->データベース.'/' .$this->wtype.'/'.$this->wpath.')';
$this->waisindex = '([wW][aA][iI][sS]://'.$this->ホストポート.'/'.$this->データベース.'[? ]'$this->検索。')';
$this->waisdatabase = '([wW][aA][iI][sS]://'.$this->ホストポート.'/'.$this->データベース.')' ;
$this->waisurl = '('.$this->waisdatabase.'|'.$this->waisindex.'|'.$this->waisdoc.')';
}
}

?>


文件otherfunc.phpの内容:
//otherfunc.php
function htmlencode($str){
$str = (string) $str;

$ret = '';
$len = strlen($str);
$nl = false;
for($i=0;$i$chr = $str[$i];
switch($chr){
case '$ret .= '$nl = false;
休憩;
case '>':
$ret .= '>';
$nl = false;
休憩;
ケース '"':
$ret .= '"';
$nl = false;
休憩;
ケース '&':
$ret .= '&';
$nl = false;
休憩;
/*
case ' ':
$ret .= ' ';
$nl = false;
休憩;
*/
case chr(9):
$ret .= ' ';
$nl = false;
休憩;
case chr(10):
if($nl) $nl = false;
else{
$ret .= '
';
$nl = true;
}
休憩;
case chr(13):
if($nl) $nl = false;
else{
$ret .= '
';
$nl = true;
}
休憩;
デフォルト:
$ret .= $chr;
$nl = false;
休憩;
}
}

return $ret;
}


function htmlencode4textarea($str){
$str = (string)$str;

$ret = '';
$len = strlen($str);
for($i=0;$i$chr = $str[$i];
switch($chr){
case '$ret .= '休憩;
case '>':
$ret .= '>';
休憩;
ケース '"':
$ret .= '"';
休憩;
ケース '&':
$ret .= '&';
休憩;
case ' ':
$ret .= ' ';
休憩;
case chr(9):
$ret .= ' ';
休憩;
デフォルト:
$ret .= $chr;
休憩;
}
}

return $ret;
}

関数 emailcheck($email){
$ret=false;
if(strstr($email, '@' ) && strstr($email, '.')){
if(eregi("^([_a-z0-9] ([._a-z0- 9-] )*)@([a-z0-9]{2,}(.[a-z0-9-]{2,})*.[a-z]{2,3})$", $email )){
$ret=true;
}
}
return $ret;
}

function str2url($path){
return eregi_replace("/","/",urlencode($path));
}
?>

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。