Jump to content


Photo
* * * * * 1 votes

[PHP] BBCode Parser


  • Please log in to reply
40 replies to this topic

#21 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 25 December 2005 - 04:32 PM

Ok, posting it now, be up in 5 minutes

Could anyone post a link for the code they refer to? Really looking for it, but couldn't find it when I searched the forums :lol:

#22 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 25 December 2005 - 11:40 PM

Believe me, i've done my fair share of work with regular expressions... You don't need (?i) to make the expression case-insensative. If you don't want to take my word, take php.net's word.
http://php.net/manua...tern.syntax.php

And eh, indigo, have you tried searching the tutorial database...?
http://www.pixel2lif...&d=1&ss=smilies

Edited by rc69, 25 December 2005 - 11:44 PM.


#23 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 26 December 2005 - 07:21 PM

Thanks rc69, I feel quite stupid now. I thought it was supposed to be a tutorial posted in this forum, that's why I got confused.

But can't you guys just find something that works properly, and then post that code? Or do they all work just fine?

#24 Hayden

Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

Posted 18 March 2006 - 07:28 PM

function BBCode ($string) {
 $search = array(
     '#\[b\](.*?)\[/b\]#',
     '#\[i\](.*?)\[/i\]#',
     '#\[u\](.*?)\[/u\]#',
     '#\[img\](.*?)\[/img\]#',
     '#\[url=(.*?)\](.*?)\[/url\]#',
     '#\[code\](.*?)\[/code\]#'
 );
 $replace = array(
     '<b>\\1</b>',
     '<i>\\1</i>',
     '<u>\\1</u>',
     '<img src="\\1">',
     '<a href="\\1">\\2</a>',
     '<code>\\1</code>'
 );
 return preg_replace($search.'si', $replace, $string);
}


i copied this EXACTLY as is and it didn't work.

[Sat Mar 18 18:23:47 2006] [error] PHP Warning: preg_replace(): Unknown modifier ']' in /home/haydenk/public_html/test/bbcode.php on line 21
[Sat Mar 18 18:23:47 2006] [error] PHP Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 27 in /home/*/public_html/test/bbcode.php on line 21
[Sat Mar 18 18:23:47 2006] [error] PHP Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 20 in /home/*/public_html/test/bbcode.php on line 21
[Sat Mar 18 18:23:47 2006] [error] PHP Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 16 in /home/*/public_html/test/bbcode.php on line 21
[Sat Mar 18 18:23:47 2006] [error] PHP Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 16 in /home/*/public_html/test/bbcode.php on line 21
[Sat Mar 18 18:23:47 2006] [error] PHP Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 16 in /home/*/public_html/test/bbcode.php on line 21


got those in the error_log


Acually an even better way to do it would be to have it like so.

function BBCode ($string) {
$search = array(
	'@\[(?i)b\](.*?)\[/(?i)b\]@si',
	'@\[(?i)i\](.*?)\[/(?i)i\]@si',
	'@\[(?i)u\](.*?)\[/(?i)u\]@si',
	'@\[(?i)img\](.*?)\[/(?i)img\]@si',
	'@\[(?i)url=(.*?)\](.*?)\[/(?i)url\]@si',
	'@\[(?i)code\](.*?)\[/(?i)code\]@si'
);
$replace = array(
	'<b>\\1</b>',
	'<i>\\1</i>',
	'<u>\\1</u>',
	'<img src="\\1">',
	'<a href="\\1">\\2</a>',
	'<code>\\1</code>'
);
return preg_replace($search , $replace, $string);
}


replaced it with this and it works fine.

http://spaceghost.cc/test/bbcode.php

#25 dEcade

dEcade

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 1,850 posts
  • Gender:Male
  • Location:Saskatoon, Saskatchewan
  • Interests:Guitar, Programming, Storm Chasing, Games (Designing and playing), Hockey, Photography

Posted 18 March 2006 - 07:38 PM

Here is the way I do it and it works very nicley.

<?php
function bbcode ($string)
{
	// All the default bbcode arrays.
	$bbcode = array(
		//Text Apperence
		'#\[b\](.*?)\[/b\]#si' => '<b>\\1</b>',
		'#\[i\](.*?)\[/i\]#si' => '<i>\\1</i>',
		'#\[u\](.*?)\[/u\]#si' => '<u>\\1</u>',
		'#\[s\](.*?)\[/s\]#si' => '<strike>\\1</strike>',
		//Font Color
		'#\[color=(.*?)\](.*?)\[/color\]#si' => '<font color="\\1">\\2</font>',
		//Text Effects
		'#\[bl\](.*?)\[/bl\]#si' => '<blink>\\1</blink>',
		'#\[marquee\](.*?)\[/marquee\]#si' => '<marquee>\\1</marquee>',
		//Other
		'#\[code\](.*?)\[/ code]#si' => '<div class="bbcode_code_title">CODE:</div><div class="bbcode_code_code">\\1<div>',
		'#\[url=http://(.*?)\](.*?)\[/url]#si' => '<a href="\\1" target="_blank">\\2</a>',
		'#\[quote\](.*?)\[/quote\]#si' => '<div class="bbcode_quote_title">CODE:</div><div class="bbcode_quote_quote">\\1<div>',
		'#\[img\](.*?)\[/img\]#si' => '<img src="\\1">',
		'#\[email\](.*?)\[/email\]#si' => '<a href="mailto:\\1">\\1</a>'
	);
	$output = preg_replace(array_keys($bbcode), array_values($bbcode), $string);
	return $output;
}
?>

NOTCE: there is a space for the [/code] part so it looks like this [/ code] so that it code doesn't look funny. if you use it just take the space out.

It should work then to use the bbcode you would do this:

bbcode('stuff here [b]bold text[/b]');

So hope that helps you guys out.

dEcade

Edited by dEcade, 18 March 2006 - 07:40 PM.


#26 Hayden

Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

Posted 18 March 2006 - 09:35 PM

Nice dEcade. :)



Can anybody tell me how to add the smilie bbcode? :lol:

#27 dEcade

dEcade

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 1,850 posts
  • Gender:Male
  • Location:Saskatoon, Saskatchewan
  • Interests:Guitar, Programming, Storm Chasing, Games (Designing and playing), Hockey, Photography

Posted 19 March 2006 - 09:07 AM

Well I haven't try this but basically I think you would do something like this on the page that your forum thing submits to.

<?php
function set_smiley($string)
{
$new_string = str_replace(':)', 'SMILEY HTML HERE');
return $new_string;
}
?>

Then you would run the script when you posted it so something like this:

Note: This is the whole script put together.

<?php
function set_smiley($string)
{
$new_string = str_replace(':)', 'SMILEY HTML HERE');
return $new_string;
}

if(isset($_POST['submit'])
{
$post = set_smiley($_POST['post']); // Notice your text area must be named post.
echo $post;
}
else
{
echo 'Your Forum Goes Here';
}
?>

If you are trying this script out make sure that the submit button is named submit other wise it won't submit. If it doesn't replace the : ) with the SMILEY HTML HERE or your html then try a preg_replace or something. This script is not tested so it may not work.

dEcade

#28 Hayden

Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

Posted 21 March 2006 - 05:24 PM

hmmm, okay.


i was thinking there'd be a way to integrated into the bbcode function. :D

#29 Boom

Boom

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 22 March 2006 - 05:56 AM

great tutorial, thanks.

#30 rus321

rus321

    Young Padawan

  • Members
  • Pip
  • 49 posts

Posted 26 March 2006 - 05:22 AM

just make 2 diffrent functions:

bbcode(set_smiley('text'));

#31 Av-

Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,972 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 26 March 2006 - 07:04 AM

Now how would you turn plain http://mysite.com text into a link, just like on this forum, without messing up the url bbcode

#32 Mr. Jay

Mr. Jay

    Young Padawan

  • Members
  • Pip
  • 81 posts

Posted 02 March 2007 - 12:51 AM

Acually an even better way to do it would be to have it like so.

function BBCode ($string) {
$search = array(
	'@\[(?i)b\](.*?)\[/(?i)b\]@si',
	'@\[(?i)i\](.*?)\[/(?i)i\]@si',
	'@\[(?i)u\](.*?)\[/(?i)u\]@si',
	'@\[(?i)img\](.*?)\[/(?i)img\]@si',
	'@\[(?i)url=(.*?)\](.*?)\[/(?i)url\]@si',
	'@\[(?i)code\](.*?)\[/(?i)code\]@si'
);
$replace = array(
	'<b>\\1</b>',
	'<i>\\1</i>',
	'<u>\\1</u>',
	'<img src="\\1">',
	'<a href="\\1">\\2</a>',
	'<code>\\1</code>'
);
return preg_replace($search , $replace, $string);
}

it will make the tags not so "case sensitive".

So that would still output the image.


The URL BBCode doesnt work for me :D

I tried this, but it stays the same :o
[url=http://www.google.com/]GOOGLE[/url]


Edit: I replaced the url BBCODE with this 1 and it works :)
'@\[url\s*=\s*(.*?)\s*\](.*?)\[\/url\]@si',

Edited by Jonpopnycorn, 07 March 2007 - 11:24 AM.


#33 SecondV

SecondV

    Young Padawan

  • Members
  • Pip
  • 28 posts
  • Gender:Male
  • Location:Kentucky
  • Interests:All things PHP &amp; MySQL :)

Posted 23 March 2007 - 11:10 PM

<?php

function parse_bbcode($text, $xhtml = true)
{
	$tags = array(
		'#\[b\](.*?)\[/b\]#si' => ($xhtml ? '<strong>\\1</strong>' : '<b>\\1</b>'),
		'#\[i\](.*?)\[/i\]#si' => ($xhtml ? '<em>\\1</em>' : '<i>\\1</i>'),
		'#\[u\](.*?)\[/u\]#si' => ($xhtml ? '<span style="text-decoration: underline;">\\1</span>' : '<u>\\1</u>'),
		'#\[s\](.*?)\[/s\]#si' => ($xhtml ? '<strike>\\1</strike>' : '<s>\\1</s>'),
		'#\[color=(.*?)\](.*?)\[/color\]#si' => ($xhtml ? '<span style="color: \\1;">\\2</span>' : '<font color="\\1">\\2</font>'),
		'#\[img\](.*?)\[/img\]#si' => ($xhtml ? '<img src="\\1" border="0" alt="" />' : '<img src="\\1" border="0" alt="">'),
		'#\[url=(.*?)\](.*?)\[/url\]#si' => '<a href="\\1" title="\\2">\\2</a>',
		'#\[email\](.*?)\[/email\]#si' => '<a href="mailto:\\1" title="Email \\1">\\1</a>',
		'#\[code\](.*?)\[/code\]#si' => '<code>\\1</code>',
		'#\[align=(.*?)\](.*?)\[/align\]#si' => ($xhtml ? '<div style="text-align: \\1;">\\2</div>' : '<div align="\\1">\\2</div>'),
		'#\[br\]#si' => ($xhtml ? '<br style="clear: both;" />' : '<br>'),
	);

	foreach ($tags AS $search => $replace)
	{
		$text = preg_replace($search, $replace, $text);
	}
	return $text;
}

function parse_smilies($text)
{
	$smilies = array(
		':)' => '<img src="images/smilies/smile.gif" border="0" alt="Smile" title="Smile" />',
		':(' => '<img src="images/smilies/sad.gif" border="0" alt="Sad" title="Sad" />',
		':D' => '<img src="images/smilies/biggrin.gif" border="0" alt="Big Grin" title="Big Grin" />',
		':S' => '<img src="images/smilies/confused.gif" border="0" alt="Confused" title="Confused" />'
		// etc
	);

	foreach ($smilies AS $search => $replace)
	{
		$text = str_replace($search, $replace, $text);
	}
	return $text;
}

?>

<?php

echo parse_bbcode('[align=center][b][color=red]Testing[/color][/b][br]');

?>

<div style="text-align: center;"><strong><span style="color: red;">Testing</span></strong><br style="clear: both;" /></div>


^_^

Edited by SecondV, 23 March 2007 - 11:13 PM.


#34 Nightscream

Nightscream

    Young Padawan

  • Members
  • Pip
  • 100 posts
  • Gender:Male
  • Location:Belgium

Posted 02 May 2007 - 04:56 PM

ok just read this, looks great, testing it later
but a question why do you have to do
@\[(?i)b\](.*?)\[/(?i)b\]@si

why use those ugly marks?

and why \\1? where does the \\ stand for and the 1?
<b>\\1</b>

Edited by Nightscream, 02 May 2007 - 04:58 PM.


#35 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 06 May 2007 - 01:53 AM

http://www.pixel2lif...p...st&p=114029

Read the above post, and if you want, the next few posts after that. Then to understand the backslashes, first you have to understand Regex. You can read php.net's manual on Patter Syntax for info about that. But i find the whole thing to be confusing when taken in a large dose. So, to answer your question, scroll down to the section on "Back References" and understand that inside of a string, in order to get a single backslash, you need two.

#36 Nightscream

Nightscream

    Young Padawan

  • Members
  • Pip
  • 100 posts
  • Gender:Male
  • Location:Belgium

Posted 06 May 2007 - 04:06 PM

ty i'll try that! :P

#37 Balor

Balor

    PHP Nerd

  • Members
  • Pip
  • 63 posts
  • Gender:Male
  • Location:Germany-&gt;Frankfurt
  • Interests:My beautyful girl, my son and webcoding. I'm also very interested in art but I'm not really good at art... it's sad but true ^^

Posted 27 August 2007 - 03:07 AM

Shouldn't you first strip all html tags?

I did also a bbcode script some time ago, here is what I did, it's almost the same imho:

function bbcode($text) { 
		
		$text = strip_tags($text, '<br>');
	
		$suche = array('/\[b\](.+?)\[\/b\]/i', 
					   '/\[i\](.+?)\[\/i\]/i', 
					   '/\[u\](.+?)\[\/u\]/i', 
					   '/\[url=(.+?)\](.+?)\[\/url\]/i',
					   '/\[file(.+?)\|(.+?)\]/i',
					   '/\:\)/i',
					   '/\;\)/i',
					   '/\:D/i',
					   '/\:p/i',
					   '/\:\'\(/i',
					   '/\[key\](.+?)\[\/key\]/ie',
					   '/\[quote\](.+?)\[\/quote\]/is',
					   '/\[color=(.+?)\](.+?)\[\/color\]/i');
		
		$code = array('<strong>$1</strong>',
					  '<i>$1</i>',
					  '<u>$1</u>',
					  '<img src="images/bigger.gif" alt="new_window" width="12px" height="12px" /><a href="$1" class="link" target="_blank">$2</a>',
					  '<a href="view.php?id=$1" class="link">$2</a>',
					  '<img src="images/smilies/smile.gif" alt=":)" />',
					  '<img src="images/smilies/wink.gif" alt=";)" />',
					  '<img src="images/smilies/laugh.gif" alt=":D" />',
					  '<img src="images/smilies/tounge.gif" alt=":p" />',
					  '<img src="images/smilies/worried.gif" alt=":\'(" />',
					  '$test->create_serial("$1")',
					  '<br /><div class="quote">$1</div><br />',
					  '<span style="color: $1;">$2</span>');
		
		$text = stripslashes($text); 
		$text = preg_replace($suche, $code, $text);
		$text = nl2br($text);
		
		return $text;
	}

Edited by Balor, 27 August 2007 - 03:07 AM.


#38 killeredge

killeredge

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 31 August 2007 - 09:38 PM

Now how would you turn plain http://mysite.com text into a link, just like on this forum, without messing up the url bbcode


i have created an OOP BBCode Parser that also auto-parses links with and without the [url] tag.

inside the main class i have
// Automatically parse urls
		   $code = preg_replace_callback("#(^|\s|>)((http|https|news|ftp)://\w+[^\s\[\]\<]+)#i", array( &$this, 'autourl' ), $code);

and to complete this, i have a function called autourl, that formats the url into forum-friendly and user-friendly urls. :) B) :D

#39 _*Ultimate`_*

_*Ultimate`_*
  • Guests

Posted 09 January 2008 - 02:11 AM

Wow, cheers for this! wasn't thinking it would be this easy hahaha, did have an idea on what to do though =]

#40 Mr.Shawn

Mr.Shawn

    Young Padawan

  • Members
  • Pip
  • 15 posts
  • Gender:Male
  • Location:London, Emirates Stadium

Posted 23 June 2008 - 02:04 AM

Nice one here.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users