改良された UBB class_PHP チュートリアル
Jul 21, 2016 pm 04:02 PM
<?php
/*
転載する場合は作者を明記してください
原著者:何志強
改善:SonyMusic[ sonymusic@163.net ]
ファイル:ubb.php
備考:改善と言われていますが、しかし実際には、コア関数 parse() は完全に書き直されており、考え方が異なります。
ただし、それでも He Zhiqiang の例からインスピレーションを受けており、テスト例や URLCHECK などのいくつかの関数も He Zhiqiang のプログラムに従っています。ありがとうございます。
カラー機能はまだありませんが、追加していきます。
プログラムにバグや不都合がある場合は、メールでお知らせください。
ありがとうございます!
改善された機能:
文字列の UBB エンコーディング。このクラスは現在、次の単純で実用的なエンコーディングのみをサポートしています:
1. URL リンク
[url] http://phpuser.com/ [/url]
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]タイトルNo.1[/h1] .. . [h6] タイトルワード 6[/h6]
[sup][/sup]
[sub][/sub]
[tt][/tt]
[s][/s]
[strike][ / ストライク]
[em][/em]
[strong][/strong]
[code][/code]
[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 ubbcode{
var $call_time=0;
//可处理标签及处理関数数对应表
var $tags = array( //小写的标签 => 对应的处理関数数
'url' => '$this->url',
'email' => '$this->メール',
'img' => '$this->img',
'b' => '$this->シンプル',
'i' => ; '$this->シンプル'、
'u' => '$this->シンプル'、
's' => $this->simple',
'strike' => '$this->simple',
'h1' => '$this->simple',
'h2' => ->シンプル'、
'h3' => '$this->シンプル'、
'h4' => '$this->シンプル'、
'h5' => ;シンプル',
'h6' => '$this->シンプル',
'sup' => '$this->シンプル',
'sub' => ',
'em' => '$this->シンプル',
'strong' => '$this->シンプル',
'コード' =>
'samp' =>'$this->simple'、
'kbd' =>'$this->simple'、
'var' =>'$this->simple'、
' dfn' => '$this->シンプル'、
'cite' => '$this->シンプル'、
'小さい' => '$this->シンプル'、
'大きい' => '$this->シンプル',
'ブリンク' => '$this->シンプル'
);
//url接続プロパティ
var $attr_url;
//url 合法性查对オブジェクト
var $urlcheck;
function ubbcode($attr_url){
$this->attr_url = ''.$attr_url;
$this->urlcheck = 新しい urlcheck();
}
//对$str进行UBB编码解析
function parse($str){
$this->call_time++;
$parse = ''.htmlencode($str);
$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=="email") または ($tag=="img")) および ($this->call_time>1)){
echo $this->call_time."<br>";
return $parse;//結果が不可能な場合は、嵌合できない标记、直接返されます
}
$parse2 = substr($parse,0,$pos);//标记の前
$parse = substr($parse,$ pos+$tag_len+2);//标记之後
if(!isset($this->tags[$tag])){
echo "$tag_start<br>";
$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.'") ;');
}
$this->call_time--;
$ret を返す;
}
function simple($start,$end,$value){
return '<'.$start.'>'.$value.'</'.$end.'>';
}
関数 url($start,$end,$value){
$trim_value=trim($value);
if (strto lower(substr($trim_value,0,7))!="http://")
$trim_value="http://".$trim_value;
if($this->urlcheck->check($trim_value)) return '<a href="'.$trim_value.'" '.$this->attr_url.'>'.$value. '</a>';
それ以外の場合は、'['.$start.']'.$value.'[/'.$end.']' を返します。
}
関数 email($start,$end,$value){
if(emailcheck($value)) '<a href="mailto:'.$value.'">'.$value を返します。 '</a>';
それ以外の場合は、'['.$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 '<img src ="'.$trim_value.'"></img>';
それ以外の場合は、'['.$start.']'.$value.'[/'.$end.']' を返します。
}
}
//テスト
echo '<html>';
echo '<head<title>テスト</title></head>';
echo '<body>; ;form action="'.str2url($PATH_INFO).'" method="post">';
echo '<textareacols="100" rows="10" name="ubb">'. ($ubb).'</textarea><br>';
echo '<input type="submit" value="Convert">';
if (isset($ubb)){
$ubbcode = new ubbcode('target="_blank"');
echo '<hr>'.$ubbcode->parse($ubb)
}
echo ' </body>';
echo '</html>'
?>
ファイルの内容:
<?php
//urlcheck.php
class urlcheck{
var $ regex = array(//プロトコル名 (ここでは小文字で記述する必要があることに注意してください) => 対応する正規表現
'ftp' => '$this->ftpurl',
'file' => '$ this->fileurl'、
'http' =>'$this->httpurl'、
'https' =>'$this->httpurl'、
'gopher' => >gopherurl'、
'news' => '$this->newsurl'、
'nntp' => '$this->nntpurl'、
'telnet' => telneturl',
'wais' => '$this->waisurl'
var $alpha;
var $safe;
var $hex;
var $xchar; ;
var $user;
var $toplabel;
//ftp var $ftptype;
var $fpath;
var $fileurl;
var $hpath; ;
var $httpurl;
var $gtype;
var $grouppart; ;
var $newsurl;
//nntpurl
//wais
var $wtype; ;
var $waisdatabase;
function check($url){
$pos ($url,' :',1); false を返す;
$prot = substr($url,0,$pos);
if(!isset($this->regex[$prot])) false を返す;
eval('$regex = '.$this) ->regex[$prot].';');
return ereg('^'.$regex.'$',$url);
function urlcheck(){
$this->lowalpha = '[a-z]';
$this->hialpha = '[A-Z]';
$this->alpha = '('.$this->lowalpha.'|'.$this->hialpha. ')';
$this->数字 = '[0-9]';
$this->安全 = '[$.+_-] '; )'!,]';
$this->national = '([{}|^~`]|[|])';
$this->句読点 = '[<>#%"] ';
$this->予約 = '[?;/: @&= ]';
$this->hex = '('.$this ->digit.'|[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->alphadigit = '('.$this->alpha.'|'.$this->digital.')';
$this->toplabel = '('.$this->alpha.'|('.$this->alpha.'('.$this->alphadigit.'|-)*'.$ this->英数字。'))';
$this->ドメインラベル = '('.$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->パスワード.')?@)?'.$this->ホストポート.')' ;
$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.'(%09'.$this->search.'(%09'.$this->gopher_string.')?)?)?)?) ?)';
$this->article = '(('.$this->uchar.'|[;/?:&=]) +@'.$this->host.')';
$this->group = '('.$this->alpha.'('.$this->alpha.'|'.$this->digit.'|[-.+_]) *)';
$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の内容:
<?php
//otherfunc.php
function htmlencode($str){
$str = (string)$str;
$ret = '';
$len = strlen($str);
$nl = false;
for($i=0;$i<$len;$i++){
$chr = $str[$i];
switch($chr){
case '<':
$ret .= '<';
$nl = false;
休憩;
ケース '>':
$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 .= '<br>';
$nl = true;
}
休憩;
case chr(13):
if($nl) $nl = false;
else{
$ret .= '<br>';
$nl = true;
}
休憩;
デフォルト:
$ret .= $chr;
$nl = false;
休憩;
}
}
$right を返す;
}
function htmlencode4textarea($str){
$str = (string)$str;
$right = '';
$len = strlen($str);
for($i=0;$i$chr = $str[$i];
switch($chr){
case '<':
$ret .= '<';
休憩;
ケース '>':
$ret .= '>';
休憩;
ケース '"':
$ret .= '"';
休憩;
ケース '&':
$ret .= '&';
休憩;
case ' ':
$ret .= ' ';
休憩;
case chr(9):
$ret .= ' ';
休憩;
デフォルト:
$ret .= $chr;
休憩;
}
}
$right を返す;
}
関数 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;
}
}
$right を返す;
}
function str2url($path){
return eregi_replace("%2f","/",urlencode($path));
}
?>

人気の記事

人気の記事

ホットな記事タグ

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

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

ホットトピック











Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド

PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法
