Simple PHP BBCode Parser

テキスト処理文字列

概要

 閲覧数:1097  投稿日:2016-02-18  更新日:2016-02-18  

Simple PHP BBCode Parser


コード

<?php
function bbcodeParser($bbcode){
/*
*
*	bbCode Parser
*
*	Syntax: bbcodeParser(bbcode)
*/

/*
Commands include
* bold
* italics
* underline
* typewriter text
* strikethough
* images
* urls
* quotations
* code (pre)
* colour
* size
*/

/* Matching codes */
$urlmatch = "([a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+)";

/* Basically remove HTML tag's functionality */
$bbcode = htmlspecialchars($bbcode);

/* Replace "special character" with it's unicode equivilant */
$match["special"] = "/\�/s";
$replace["special"] = '&#65533;';

/* Bold text */
$match["b"] = "/\[b\](.*?)\[\/b\]/is";
$replace["b"] = "<b>$1</b>";

/* Italics */
$match["i"] = "/\[i\](.*?)\[\/i\]/is";
$replace["i"] = "<i>$1</i>";

/* Underline */
$match["u"] = "/\[u\](.*?)\[\/u\]/is";
$replace["u"] = "<span style=\"text-decoration: underline\">$1</span>";

/* Typewriter text */
$match["tt"] = "/\[tt\](.*?)\[\/tt\]/is";
$replace["tt"] = "<span style=\"font-family:monospace;\">$1</span>";

$match["ttext"] = "/\[ttext\](.*?)\[\/ttext\]/is";
$replace["ttext"] = "<span style=\"font-family:monospace;\">$1</span>";

/* Strikethrough text */
$match["s"] = "/\[s\](.*?)\[\/s\]/is";
$replace["s"] = "<span style=\"text-decoration: line-through;\">$1</span>";

/* Color (or Colour) */
$match["color"] = "/\[color=([a-zA-Z]+|#[a-fA-F0-9]{3}[a-fA-F0-9]{0,3})\](.*?)\[\/color\]/is";
$replace["color"] = "<span style=\"color: $1\">$2</span>";

$match["colour"] = "/\[colour=([a-zA-Z]+|#[a-fA-F0-9]{3}[a-fA-F0-9]{0,3})\](.*?)\[\/colour\]/is";
$replace["colour"] = $replace["color"];

/* Size */
$match["size"] = "/\[size=([0-9]+(%|px|em)?)\](.*?)\[\/size\]/is";
$replace["size"] = "<span style=\"font-size: $1;\">$3</span>";

/* Images */
$match["img"] = "/\[img\]".$urlmatch."\[\/img\]/is";
$replace["img"] = "<img src=\"$1\" />";

/* Links */
$match["url"] = "/\[url=".$urlmatch."\](.*?)\[\/url\]/is";
$replace["url"] = "<a href=\"$1\">$2</a>";

$match["surl"] = "/\[url\]".$urlmatch."\[\/url\]/is";
$replace["surl"] = "<a href=\"$1\">$1</a>";

/* Quotes */
$match["quote"] = "/\[quote\](.*?)\[\/quote\]/ism";
$replace["quote"] = "<div class=\"bbcode-quote\">�$1�</div>";

$match["quote"] = "/\[quote=(.*?)\](.*?)\[\/quote\]/ism";
$replace["quote"] = "<div class=\"bbcode-quote\"><span class=\"bbcode-quote-user\" style=\"font-weight:bold;\">$1 said:</span><br />�$2�</div>";

/* Parse */
$bbcode = preg_replace($match, $replace, $bbcode);

/* New line to <br> tag */
$bbcode=nl2br($bbcode);

/* Code blocks - Need to specially remove breaks */
function pre_special($matches)
{
	$prep = preg_replace("/\<br \/\>/","",$matches[1]);
	return "�<pre>$prep</pre>�";
}
$bbcode = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism","pre_special",$bbcode);


/* Remove <br> tags before quotes and code blocks */
$bbcode=str_replace("�<br />","",$bbcode);
$bbcode=str_replace("�","",$bbcode); //Clean up any special characters that got misplaced...

/* Return parsed contents */
return $bbcode;
}
?>



タグ


preg_replace 





「1文字目」から「最初の指定文字」前までの文字列を取得

「半角スペースもしくは全角スペースで区切られた文字列」先頭に指定文字列を付与



週間人気ページランキング / 4-12 → 4-18
順位 ページタイトル抜粋 アクセス数
1 URLから、トップページ(index.html)判定 | ルーティング 10
2 配列キー内に「指定文字列が含まれるキー」と「その値」を抽出して、新しい配列を返す | 配列(型) 9
2 後ろから3文字削除 / 「18:00:00」→「18:00」 | 文字列(テキスト処理) 9
3 現在WebページのURLパスを第2階層まで取得 | パス 7
3 配列の空要素を削除(コールバック関数未使用) … array_filter() | 配列(型) 7
4 多次元配列の「一次元要素数」もしくは「全次元要素数」をカウント | 多次元配列(型) 4
4 指定文字列より前を取得 | 文字列(テキスト処理) 4
4 投稿日時をTwitterのように「★分前」「★時間前」という文字列変換するユーザ定義PHP関数 | 日付および時刻関連 4
5 null代入とunset()の違い | 変数 3
5 「input type="datetime-local"」で受け取った文字列をハイフンへ変更 | 日付および時刻関連 3
6 (データベースなどから取得した)多次元配列の値と、一次元配列を比較して、「一致」及び「増減」を返す | 配列(型) 2
6 MySQLのdatetime型カラムに格納している値と比較して、24時間以内かどうかを判定 | 日付および時刻関連 2
6 range( ) を使用して日付期間を配列生成 | 日付および時刻関連 2
6 多次元配列キーを、多次元配列内にある「特定キーの値」へ変更 | 多次元配列(型) 2
6 $_SERVER['REQUEST_URI'] | タグ毎エントリー一覧 2
7 array_column | タグ毎エントリー一覧 1
7 配列内容を、foreachでテーブル表示 | テーブル 1
7 配列の空要素を削除(コールバック関数利用) … array_filter() | 配列(型) 1
7 switch文判定 … 「in_array」「array_keys」 | 配列(型) 1
7 PHPで最後の「指定区切り文字」より後ろを取得 | 配列(型) 1
2024/4/19 1:02 更新