Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Use Bb Code On Your Site!, Just like on forums!
Unholy Prayer
post Apr 7 2007, 11:17 PM
Post #1


Newbie [Level 2]
**

Group: Members
Posts: 36
Joined: 27-December 05
Member No.: 16,252



To use this you must have PHP support on your server. Just use this code:

CODE
<?php

$content = "Hello, World!";

$html = array('[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]');
$replacements = array ('<b>', '</b>', '<i>', '</i>', '<u>', '</u>');

$content = str_replace($html, $replacements, $content);

?>


This code can be very useful. It can be used for word filters to block the use of bad words on your site or for emoticons.
Go to the top of the page
 
+Quote Post
Imtay22
post Apr 9 2007, 04:17 PM
Post #2


Super Member
*********

Group: Members
Posts: 293
Joined: 27-January 07
From: Winter is cold here.
Member No.: 37,984
Spam Patrol



So I can just create a php file called whatever.php and put that code in it?
Go to the top of the page
 
+Quote Post
Blessed
post Apr 9 2007, 09:16 PM
Post #3


Advanced Member
*******

Group: Members
Posts: 144
Joined: 22-March 07
Member No.: 40,472



yes you can m8

but make sure to put it out like this

$content = str_replace($html, $replacements, $content);
Go to the top of the page
 
+Quote Post
galexcd
post Apr 13 2007, 10:02 PM
Post #4


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,012
Joined: 25-September 05
From: L.A.
Member No.: 12,251



str replace is bad for bbcode especially when it comes to urls etc.

If you use preg_replace, you can get php to check to make sure it actually has an ending. That way if sombody leaves a [b] tag open, it dosn't parse it out. They would have to close it.

Here's the bb code i use on the forums i coded.

CODE
$main_search=array('/\[b\](.*?)\[\/b\]/is','/\[i\](.*?)\[\/i\]/is','/\[u\](.*?)\[\/u\]/is','/\[img\](.*?)\[\/img\]/is','/\[url\=(.*?)\](.*?)\[\/url\]/is','/\[url\](.*?)\[\/url\]/is','/\[color\=(.*?)\](.*?)\[\/color\]/is','/\[code\](.*?)\[\/code\]/is');
$main_replace=array('<strong>$1</strong>','<em>$1</em>','<u>$1</u>','<img src="$1" style="max-width: 100%" border="0">','<a href="$1">$2</a>','<a href="$1">$1</a>','<font color=$1>$2</font>','<div style="WIDTH: 100%; OVERFLOW: auto"><pre>$1</pre></div>');
$str=preg_replace ($main_search, $main_replace, $str);



Str_replace is good for smilies though!
Go to the top of the page
 
+Quote Post
Imtay22
post Apr 13 2007, 10:08 PM
Post #5


Super Member
*********

Group: Members
Posts: 293
Joined: 27-January 07
From: Winter is cold here.
Member No.: 37,984
Spam Patrol



Ok! Do they both work though? I used another tutorial here that used preg_replace. They also said it was more better than str_replace. Unholy, you might want ot take his advice!
Go to the top of the page
 
+Quote Post
galexcd
post Apr 13 2007, 10:12 PM
Post #6


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,012
Joined: 25-September 05
From: L.A.
Member No.: 12,251



QUOTE(Imtay22 @ Apr 13 2007, 03:08 PM) *
Ok! Do they both work though? I used another tutorial here that used preg_replace. They also said it was more better than str_replace. Unholy, you might want ot take his advice!


More better? lol

If you use preg_replace you'll get a much more professional bb parser. It would be more like trap17's bb. This would be the effect you would get:

[b]if this board used str_replace to parse bb code, the rest of this post would be bold because the bold tag would be parsed and replaced into <b> no matter what.


Go to the top of the page
 
+Quote Post
Imtay22
post Apr 13 2007, 10:18 PM
Post #7


Super Member
*********

Group: Members
Posts: 293
Joined: 27-January 07
From: Winter is cold here.
Member No.: 37,984
Spam Patrol



Ahhh.... Okay. Thanks. I think I will stick with the easyer one.
Go to the top of the page
 
+Quote Post
galexcd
post Apr 18 2007, 03:37 AM
Post #8


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,012
Joined: 25-September 05
From: L.A.
Member No.: 12,251



The problem with using str_replace for tags like img is you would have to have [img] replace to <img src=" and [ /img] replace to ">. Now if you think about it, if you use that, anybody could easily break your forums by just starting an image tag without ending it. If you write <img src=" in the middle of your code, it's not going to end untill it sees another quote and another greaterthan. If your site uses tables, this could really screw up the site.

P.S.
Along with emoticons, str_replace is really good for disabling HTML.

CODE
$string=str_replace(array("<",">"),array("&lt;","&gt;"),$string);
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How do you test your php code(77)
  2. Php Clock(7)
  3. Change Permission With Php Code(3)
  4. Wap Forum(9)
  5. Adapting Html Code Embed To Work On Phpnuke(7)
  6. Dynamic Image / Signature Generator(12)
  7. Wap Source Code Viewer(4)
  8. Php Problems(3)
  9. Php Education Class (first Code)(0)
  10. How To Make A Random 7 Number Code?(2)
  11. Displaying Your Phone Number On A Wap Site(3)
  12. Whats Wrong>?(9)
  13. Connecting Php Site To Database(6)
  14. Html Site With Login(2)
  15. Awesome Source Code Viewer Script(7)
  1. Html Code Tester. Online Script(15)
  2. I Need Some Proof Reading For My Code Please! [resolved](7)
  3. Will This Code Work(5)
  4. Xbox Gamertag Profile On Site(2)
  5. Use Rss In Php Code(3)
  6. Php Code(15)