Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

BBCode

Format BBCode input

function bbcode_format($var) {
	$search = array(
		'/\[b\](.*?)\[\/b\]/is',                                
		'/\[i\](.*?)\[\/i\]/is',                                
		'/\[u\](.*?)\[\/u\]/is',
		'/\[img\](.*?)\[\/img\]/is',
		'/\[url\](.*?)\[\/url\]/is',
		'/\[url\=(.*?)\](.*?)\[\/url\]/is'
		);

	$replace = array(
		'<strong>$1</strong>',
		'<em>$1</em>',
		'<u>$1</u>',
		'<img src="$1" />',
		'<a href="$1">$1</a>',
		'<a href="$1">$2</a>'
		);

	$var = preg_replace ($search, $replace, $var);

	return $var;
}

a basic regex example in ruby

This is a simple regex in ruby. It returns an array of all bbcode urls.

bbcode_urls = text.scan(/\[url={0,1}.*?\].*?\[\/url\]/)

ubbcode

a function to convert common used bbcode tags to html... as usually used in message boards and some blog software.

it handles img, email, link, b, u and i tags...

for example
[b]test[/b]
[url=http://blah.com]blah[/url]

will become:
<strong>test</strong>
<a href="http://blah.com" target="_blank">blah</a>


function ubbcode($posting) {
	$posting=eregi_replace("\[img\]([^\[]+)\[/img\]","<img src=\"\\1\" border=\"0\">",$posting);
	$posting=eregi_replace("\[email\]([^\[]+)\[/email\]","<a href=\"mailto:\\1\">\\1</a>",$posting);
	$posting=eregi_replace("\[email=([^\[]+)\]([^\[]+)\[/email\]","<a href=\"mailto:\\1\">\\2</a>",$posting);
	$posting=eregi_replace("\[url=([^\[]+)\]([^\[]+)\[/url\]","<a href=\"\\1\" target=\"_blank\">\\2</a>",$posting);
	$posting=eregi_replace("\[url\]([^\[]+)\[/url\]","<a href=\"\\1\" target=\"_blank\">\\1</a>",$posting);
    $posting=eregi_replace("\[b\]","<strong>",$posting);
    $posting=eregi_replace("\[/b\]","</strong>",$posting);
    $posting=eregi_replace("\[u\]","<u>",$posting);
    $posting=eregi_replace("\[/u\]","</u>",$posting);
    $posting=eregi_replace("\[i\]","<em>",$posting);
    $posting=eregi_replace("\[/i\]","</em>",$posting);

    return $posting;
}
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS