View Single Post
 
Reply
Posted 2009-05-16, 12:11 AM in reply to Skurai's post starting "Lol, Fail quote!"
Code:
public static function bbcode($str) {
	// Optionally, do this to break up words longer than 20 characters (without breaking code)
	// $str = preg_replace("/(([^\(\)\{\}\[\]_]\S){20})/","$1 ",$str);
	// Make sure htmlentities has been run through the text and that
	// $str = preg_replace("/(\r|\n)/","<br />",$str); // has been run through
	//-----------------------------------------
	// This is part of NRG's A3 PHP Framework
	// Do not redistribute, it is strictly forbidden by the license.
	$x = 0;
	$noparse = array();
	while (preg_match("/\[noparse\](.*)\[\/noparse\]/",$str)) {
		$noparse[$x] = preg_replace("/(.*?)\[noparse\](.*?)\[\/noparse\](.*)/","$2",$str);
		$str = preg_replace("/(.*?)\[noparse\](.*?)\[\/noparse\](.*)/","$1<PARSECODE_".$x.">$3",$str);
		$x += 1;
	}
	for ($x=0;$x<count($noparse);$x++) {
		$noparse[$x] = str_replace(array(":","[","]"),array(":","[","]"),$noparse[$x]);
		$str = str_replace("<PARSECODE_".$x.">",$noparse[$x],$str);
	}
	$codes = array();
	$codes[0] = "[b]param[/b]";
	$codes[1] = "[i]param[/i]";
	$codes[2] = "[u]param[/u]";
	$codes[3] = "[a]param[/a]";
	$codes[4] = "[a=param]param[/a]";
	$codes[5] = "[a,param]param[/a]"; // This is not necessary
	$codes[6] = "[s]param[/s]";
	$codes[7] = "[subscript]param[/subscript]";
	$codes[8] = "[superscript]param[/superscript]";
	$codes[9] = "[image]param[/image]";
	$codes[10] = "[code]param[/code]";
	$codes[11] = ":smile:";
	$codes[12] = ":sad:";
	
	$replw = array();
	$replw[0] = "<b>param1</b>";
	$replw[1] = "<i>param1</i>";
	$replw[2] = "<u>param1</u>";
	$replw[3] = "<a href=''param1'' rel=''nofollow''>param1</a>";
	$replw[4] = "<a href=''param1'' rel=''nofollow''>param2</a>";
	$replw[5] = "<a href=''param2'' class=''param1''>param2</a>";
	$replw[6] = "<s>param1</s>";
	$replw[7] = "<sub>param1</sub>";
	$replw[8] = "<sup>param1</sup>";
	$replw[9] = "<image src=''param1'' alt='''' />";
	$replw[10] = "<div class=''bb_code''>param1</div>";
	$replw[11] = "<image src=''img/smilies/smile.gif'' alt='''' />";
	$replw[12] = "<image src=''img/smilies/sad.gif'' alt='''' />";

	for ($x = 0; $x < count($codes); $x++) {
		$codes[$x] = preg_replace("/(\[|\]|\/)/","\\ $1",$codes[$x]);
		$codes[$x] = preg_replace("/\s/","",$codes[$x]);
		$codes[$x] = preg_replace(array("/\A/","/\z/"),array("/","/"),$codes[$x]);
		$codes[$x] = str_replace("param","(.*?)",$codes[$x]);
		$replw[$x] = str_replace("''","\"",$replw[$x]);
		$replw[$x] = str_replace("param","$",$replw[$x]);
	}
	$str = preg_replace($codes,$replw,$str);
	return $str;
}
This is a neat little function I made for my php A3 framework. Get rid of "public static" if you want to use this as a function. Normally it's inside my framework class.

Oh, and none of this is copied from anywhere. I designed this 2-days ago on my own. So taking it without credit, is strictly forbidden since my framework costs money.

------------------------------

As a test, I wrote this.

Framework Version
<?php
require("framework/a3.framework.php");
$str = ":smile::smile:BOLD[b]BOLD[/b] Hello [i][b]Hello[/b][/i]";
echo a3::bbcode($str);
?>

This version
<?php
// Put code above here right here (of course without "public static")
$str = ":smile::smile:BOLD[b]BOLD[/b] Hello [i][b]Hello[/b][/i]";
echo bbcode($str);
?>

I like posting code crap here. Eat it up! Rate it. Byte me.

Only problem I see, is stacking noparse will break noparse.

Edit: 
Oh, I just found out that it also breaks Jelsoft's bb code system. How wonderful, I win! That means that apparently no one else has really come up with any other means of pulling that off. The way I did it is probably fairly similar to this boards way of doing it.


Outcome
:smile::smile:BOLD[b]BOLD[/b] Hello [i][b]Hello[/b][/i]
 
Work List
疲れていますから 寝むってありますね。 むずかしいです。 また、ケーキ屋で ケーキを食べていました。

I've considered being a translator, but I dunno. It feels like a lot of work. If someone gets angry then I have to deal with it, you know? I'd rather just relax.

 
Speed Test
 
Favorite Anime/Manga
#01 Clannad ~After Story~
#02 Trigun {Maximum}
#03 Koi Kaze
#04 Berserk
#05 Outlaw Star
#06 Slayers
#07 Desert Punk
#08 Spirited Away
#09 Fullmetal Alchemist
#10 Shakugan no Shana
#11 Death Note
#12 FLCL
#13 Tokyo Magnitude 8.0
#14 Toradora
#15 Gunslinger Girl

 
Anime List

Last edited by Goodlookinguy; 2009-05-16 at 12:30 AM.
Old
Profile PM WWW Search
Goodlookinguy seldom sees opportunities until they cease to beGoodlookinguy seldom sees opportunities until they cease to beGoodlookinguy seldom sees opportunities until they cease to beGoodlookinguy seldom sees opportunities until they cease to be
 
 
Goodlookinguy