Jump to content


Photo
* * * * * 1 votes

[PHP] BBCode Parser


  • Please log in to reply
40 replies to this topic

#1 HaloprO

HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 13 August 2005 - 03:56 AM

Hi.
Setup a new php document and make a function called BBCode
<?php
function BBCode ($string) {

}
?>
Then we need to use regular expressions and arrays for the search and replace
<?php
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, $replace, $string);
}
?>
That runs a search through a specified string, it tells it to look for b, i, u, img, url, and code.
It then turns it into html format.
Usuage of this function is
<?php
echo BBCode('Here is some [b]Bold![/b] and so on!');
?>
Enjoi.
  • tingying and newcymmelry like this

#2 Jaymz

Jaymz

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 4,104 posts

Posted 13 August 2005 - 09:50 AM

Thanks for sharing :P

#3 Blitz

Blitz

    Jedi In Training

  • Twodded Staff
  • PipPip
  • 307 posts
  • Location:California
  • Interests:Php, html, css, etc, band, trumpet, anime, my dog, TV, computers, video games, sleeping, marching band, sleeping, jazz, sleeping, metal, sleeping, classic rock, sleeping, music, jazz band, did I mention sleeping?, kicking the hell out of kids that won't take the time to spell or use proper grammar, my website, etc.

Posted 13 August 2005 - 12:15 PM

Thanks much! I could never figure how to do that.

#4 Jaymz

Jaymz

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 4,104 posts

Posted 13 August 2005 - 02:02 PM

There are some nice tutorials out there too to get the javascript buttons like IPB uses :hi: I use the technique on a friend's personal site (which I did a quick CMS for) and she loves it. Makes it really easy to use :)

#5 HaloprO

HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 13 August 2005 - 02:37 PM

Want me to put up a javascript tutorial on how to do it?

#6 Blitz

Blitz

    Jedi In Training

  • Twodded Staff
  • PipPip
  • 307 posts
  • Location:California
  • Interests:Php, html, css, etc, band, trumpet, anime, my dog, TV, computers, video games, sleeping, marching band, sleeping, jazz, sleeping, metal, sleeping, classic rock, sleeping, music, jazz band, did I mention sleeping?, kicking the hell out of kids that won't take the time to spell or use proper grammar, my website, etc.

Posted 14 August 2005 - 02:41 AM

Yeah, that'd be very useful if you could put up the javascript as well, that's what I've been looking for.

Oh, and just to let you know, it doesn't work. I get this error when trying to use it:

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /*****/*****/*****/*****/www/blognetwork/ben/lib/code.php on line 10

Parse error: parse error, unexpected '[', expecting ')' in /*****/*****/*****/*****/www/blognetwork/ben/lib/code.php on line 10

I think it's because you're escaping the array for the bold [b]. \' will make it so that it'll display ' in your string, but since there isn't anything to end the string, it has problems.

Edited by Blitz, 14 August 2005 - 02:45 AM.


#7 softLearner

softLearner

    Young Padawan

  • Members
  • Pip
  • 128 posts

Posted 14 August 2005 - 04:57 AM

I see the erro i think as to start his expression for find in the tags he's using a backslash at the start and end of the expression convert them to forward slashes and you'll be fine, ie:
chnage
'\[b\](.*?)\[\/b\]\',
to
'/[b\](.*?)\[\/b\]/',

Alos add is to the end each expression so it can ignore line breaks and is case-insensitve, other wise if you want multiple lines to be bolded or underlined then you may find it wont actually do anything. so it replace with:
'/[b\](.*?)\[\/b\]/is',

Doing that for all the expressions and you'll be fine.

Also those that don't knw much about regular expression the square brackets ( [] ) actaully have a special meaning, so esacaping them by adding a backslash to them will make the PCRE engine to use them as prt of what your want it to look for.

Edited by softLearner, 14 August 2005 - 09:58 AM.


#8 HaloprO

HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 15 August 2005 - 12:02 AM

Yeah it was supposed to be forward slashes not backslashes, And about the javascript, you mean the little (:) etc buttons that insert it into the text field?

#9 Blitz

Blitz

    Jedi In Training

  • Twodded Staff
  • PipPip
  • 307 posts
  • Location:California
  • Interests:Php, html, css, etc, band, trumpet, anime, my dog, TV, computers, video games, sleeping, marching band, sleeping, jazz, sleeping, metal, sleeping, classic rock, sleeping, music, jazz band, did I mention sleeping?, kicking the hell out of kids that won't take the time to spell or use proper grammar, my website, etc.

Posted 15 August 2005 - 12:17 AM

Yeah. That'd be really great.

#10 HaloprO

HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 15 August 2005 - 01:08 AM

Ok, posting it now, be up in 5 minutes

#11 Arutha

Arutha

    Young Padawan

  • Members
  • Pip
  • 144 posts
  • Gender:Male
  • Location:Southampton, England

Posted 18 August 2005 - 04:57 AM

There are some nice tutorials out there too to get the javascript buttons like IPB uses ;) I use the technique on a friend's personal site (which I did a quick CMS for) and she loves it. Makes it really easy to use :D

care to share i love new ways of doing things makes life/coding more interesting

#12 Jaymz

Jaymz

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 4,104 posts

Posted 18 August 2005 - 06:48 AM

There are some nice tutorials out there too to get the javascript buttons like IPB uses :P I use the technique on a friend's personal site (which I did a quick CMS for) and she loves it. Makes it really easy to use :)

care to share i love new ways of doing things makes life/coding more interesting

The bbcode is all admin-side and you can't tell from the site itself :unsure: But here it is :blink:

#13 Jynxis

Jynxis

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:The Shadows

Posted 01 September 2005 - 02:42 AM

hi... yeah i get an error with the code...

Heres wut i edited... well from wut i was told to do anyways.

function BBCode ($string) {
   $search = array(
       '/[b\](.*?)\[\/b\]/is',
       '/[i\](.*?)\[\/i\]/is',
       '/[u\](.*?)\[\/u\]/is',
       '/[img\](.*?)\[\/img\]/is',
       '/[url\=(.*?)\](.*?)\[\/url\]/is',
       '/[code\](.*?)\[\/code/]/is'
   );
   $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);
}


and heres teh Errors
Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 16 in /home/adminis/public_html/plague/viewtopic.php on line 133

Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 16 in /home/adminis/public_html/plague/viewtopic.php on line 133

Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 16 in /home/adminis/public_html/plague/viewtopic.php on line 133

Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 20 in /home/adminis/public_html/plague/viewtopic.php on line 133

Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 27 in /home/adminis/public_html/plague/viewtopic.php on line 133

Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 22 in /home/adminis/public_html/plague/viewtopic.php on line 133

this is a kewl bbcode function... and i really needed it.. .have been looking for awhile... it would be a shame to not have it work...lol...

#14 eVoGre3n

eVoGre3n

    Young Padawan

  • Members
  • Pip
  • 85 posts

Posted 05 September 2005 - 11:47 AM

Ya, im looking into this aswell

#15 Blitz

Blitz

    Jedi In Training

  • Twodded Staff
  • PipPip
  • 307 posts
  • Location:California
  • Interests:Php, html, css, etc, band, trumpet, anime, my dog, TV, computers, video games, sleeping, marching band, sleeping, jazz, sleeping, metal, sleeping, classic rock, sleeping, music, jazz band, did I mention sleeping?, kicking the hell out of kids that won't take the time to spell or use proper grammar, my website, etc.

Posted 05 September 2005 - 02:51 PM

PlaGuEX try this.

function BBCode ($string) {
  $search = array(
      '/\[b\](.*?)\[\/b\]/is',
      '/\[i\](.*?)\[\/i\]/is',
      '/\[u\](.*?)\[\/u\]/is',
      '/\[img\](.*?)\[\/img\]/is',
      '/\[url\=(.*?)\](.*?)\[\/url\]/is',
      '/\[code\](.*?)\[\/code/]/is'
  );
  $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);
}

He forgot to escape the [ characters. I don't know if that'll fix your problem, but it's worth a try.

#16 rc69

rc69

    PHP Master PD

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

Posted 05 September 2005 - 04:41 PM

Save space, think simple ;)
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);
}

Simply took all the "is"'s out and put it in the preg_replace. Just makes thing's that much more simple.
Also used '#' around the expression to lessen how much you need to escape (and unescaped the "=" in the url code... no need).

p.s. "is" and "si" are the exact same thing, so don't think to correct me on that. :P
Also, you may want to think about editing your original post so people can see the "correct" code and don't have to scroll down and sort through all the posts (or re-post the same error).

#17 Developer

Developer

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 10 December 2005 - 03:08 AM

here is the right code
<?php
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 , $replace, $string);
}
?>


#18 Jynxis

Jynxis

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:The Shadows

Posted 10 December 2005 - 10:30 PM

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.

#19 rc69

rc69

    PHP Master PD

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

Posted 11 December 2005 - 04:30 PM

You do know that if you have the i modifier at the end, you don't need the (?i)'s any where in the expression right?

Also, my last code is wrong... i forgot that adding .'si' would make it a string, rather then an array (which is wrong).

#20 Jynxis

Jynxis

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:The Shadows

Posted 12 December 2005 - 01:38 AM

You do know that if you have the i modifier at the end, you don't need the (?i)'s any where in the expression right?


I tried cust sticking the (?i) anywhere... and it didnt work.

all the (?i) does, is makes it not care about the "case".

I know it works cuz i tested it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users