UK Webmaster Talk - Online Marketing - SEO


 

PHP - BBcode

This is a discussion on PHP - BBcode within the Programming Articles forums, part of the Webmaster Articles/Tutorials category; Hell everybody Today i'm going to teach you how you can use the PHP function "str_replace()" to add bbcode to ...


Go Back   UK Webmaster Talk - Online Marketing - SEO > Webmaster Articles > Webmaster Articles/Tutorials > Programming Articles

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Notices

Reply

 

LinkBack Thread Tools Display Modes
Old 16-04-2005, 12:56   #1 (permalink)
WMT Addict
 
Join Date: Apr 2005
Posts: 120
iTrader: 0 / 0%
Shwaza will become famous soon enough
Default PHP - BBcode

Hell everybody Today i'm going to teach you how you can use the PHP function "str_replace()" to add bbcode to your site . You have probably noticed on several forums, or chat rooms, or other things that you can post in, that you can type in something like [B ]Text[/B ] and it will turn that text into bold, (Minus the space ofcourse though).

First of all, i'll show you a small little example, of how to make bold text, when a user types in [B ] and [/B ].

PHP Code:
<?php

//Set an origional value for $message
$message="Hello, this text is [b]Bold[/b]";

//Set values for [b] and [/b] to be changed to
$message=str_replace('[b]''<b>'$message);
$message=str_replace('[/b]''</b>'$message);

//Echo out the new value
echo $message;

?>
And that will echo out:

Hello, this text is Bold

However, that's just a very uncomplicated use for it. Say you wanted to have something like [QUOTE ], and [/QUOTE ]. You would use something like this.

PHP Code:
<?php

//Set origional value for $message
$message="[quote]Quoted text[/quote]";

//Set the new values for [quote] and [/quote]
$message=str_replace('[quote]''<table width="400" border="1">
<tr><td>Quote</td></tr><tr><td>'
$message);
$message=str_replace('[/quote]''</td></tr></table>'$message);

//Echo out the new $message
echo $message;

?>
Now, you probably might have noticed at one point or another, that if you submit something, and you're using a text area, that when you press the enter key, it won't make a <br />. Say you had a small shoutbox or something, and you wanted to allow people to make line breaks, you could use str_replace to do that.

PHP Code:
<?php

//Set a value for $message
$message="Line one

Line 2"
;

//Set value for line breaks
$message=str_replace(chr(13), '<br />'$message);

//Echo out the new $message
echo $message;

?>
And you'll see that now, we aren't converting html, we're converting chr(13). That is because the enter key is equal to chr(13), so everytime it finds a line break, it will convert it to a <br />.

Finally, i'll show you how to disallow HTML, in a submitted post. Say you didn't want to allow people to post html, because it could mess up your layout or something. You could just do this:

PHP Code:
<?php

//Set a value for $message
$message="<b>Bold Text</b>";

//Disallow html
$message=str_replace('<''&lt;'$message);
$message=str_replace('>''&gt;'$message);

//Echo the new $message
echo $message;

?>
Those will convert < and > into < and > except on the users side, not as html.

I hope this was helpful for you And if you have any questions, don't hesitate to ask!
__________________
Kingdoms Of Battle << Check out my free to play online strategy game!

SomeCoders - Coding blog, coding tips
Shwaza is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 16-04-2005, 14:38   #2 (permalink)
Tim
Cool Newbie
 
Join Date: Apr 2005
Posts: 32
iTrader: 0 / 0%
Tim is just really nice
Default

Actually, I'll be mean and critisize your ideas.
First off, what if someone decided to just put a , and not finish it with a ? Then the whole rest of your page would be bold, completely recking it. This is why most BBCode systems use pattern replacing instead of straight out string replacing.
Second, your html break codes could be simplified by using the nl2br() function.
Third, you should A: put your html remover before all of your bbcode is replaced, and B: The removing of the tags -- and a bunch of other stuff -- can be simplified with the htmlentities() function.

That's all, folks.
Tim is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 17-04-2005, 00:48   #3 (permalink)
WMT Addict
 
Join Date: Apr 2005
Posts: 120
iTrader: 0 / 0%
Shwaza will become famous soon enough
Default

And there goes tim ruining my tuts as always :P
__________________
Kingdoms Of Battle << Check out my free to play online strategy game!

SomeCoders - Coding blog, coding tips
Shwaza is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 17-04-2005, 00:49   #4 (permalink)
Tim
Cool Newbie
 
Join Date: Apr 2005
Posts: 32
iTrader: 0 / 0%
Tim is just really nice
Default

You betcha
Tim is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 17-04-2005, 02:10   #5 (permalink)
*poke* *poke*
 
Join Date: Apr 2005
Posts: 134
iTrader: 0 / 0%
mck9235 is on a distinguished road
Default

I'm never posting tutorials again, Tim is just too scary. I thought it was cool Shwaza.
__________________
| - The Fast Track - |
Thanks to WMT for the new domain!
mck9235 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 17-04-2005, 02:27   #6 (permalink)
Senior Member
 
Join Date: Apr 2005
Posts: 1,088
iTrader: 0 / 0%
Departed is a name known to allDeparted is a name known to all
Default

Yeah, nice one shwaza. Keep it up.
__________________
-Gary
Departed is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
bbcode, php

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT +1. The time now is 18:19.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
UK Webmaster Forum © WebmasterTalk.co.uk | Design by Forbairt

Ad Management by RedTyger

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41