Jump to content


Request for help about bbcode


2 replies to this topic

#1 Loser

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 29 November 2007 - 07:02 PM

Hey :)
Im want to make the bbcode. I found this small tutorial and its understandable.
It parses only "url" and "b".. I want to add more things also, for instance [I ][ /I] and [U ][/U ]. can someone help me to extend this code? I tried to do by myself and couldnt get success.
Thanks.

<?php
   $string = 'This is [b]cool[/b] - [url=http://www.yahoo.com]Yahoo website.[/url] ';
   
   $bb_replace = array (
				   '/(\[[Bb]\])(.+)(\[\/[Bb]\])/',
				   '/(\[url=)(.+)(\])(.+)(\[\/url\])/'
				   
				   );
   $bb_replacements = array (
				   '<b>\\2</b>',
				   '<a href="\\2">\\4</a>'
				   
				   );
   $string = preg_replace($bb_replace, $bb_replacements, $string);
   print $string;
   ?>


#2 nitr0x

    Young Padawan

  • Members
  • Pip
  • 201 posts

Posted 29 November 2007 - 08:44 PM

simple, the top array is for the bbcode, and the bottom array is what the bbcode gets replaced with, in this instance, [ b ] will get replaced with < b >. If you look at the end of the first array, it has a comma, you need to use this every time you add a new line to the array.

				   '/(\[[Bb]\])(.+)(\[\/[Bb]\])/',
				   '/(\[url=)(.+)(\])(.+)(\[\/url\])/',
				   '/(\[Ii]\])(.+)(\[\/[Ii]\])/'[code]

Notice I didn't add the comma on that last line but added it after the url bbcode. And to replace it.

[code]				   '<b>\\2</b>',
				   '<a href="\\2">\\4</a>',
				   '<em>\\2</em>'

Easy as that.

#3 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 30 November 2007 - 02:38 AM

There are hundreds of bbcode tutorials out there, and this has been asked plenty of times before, use the forum search next time.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users