View Full Version : Help with BB Code!

09-08-2009, 12:56 AM
Hello Guys

Im new here - and im not sure if this is the correct place to post? Well, please correct me if im wrong.

I have an Online game - and im currently making changes to the BB Code so that users can embed YouTube videos into there profiles - but im not surehow?

Here is a snippet of the code:

/////////BB Codes////////////

$txt = str_replace("
", "<fieldset><legend>QUOTE:</legend><br>", $txt);

$txt = str_replace("", "</fieldset>", $txt);

$txt = str_replace("
", "<center>", $txt);

$txt = str_replace("", "</center>", $txt);

$txt = str_replace("", "<b>", $txt);

$txt = str_replace("", "</b>", $txt);

$txt = str_replace("", "<i>", $txt);

$txt = str_replace("", "</i>", $txt);

$txt = str_replace("", "<u>", $txt);

$txt = str_replace("", "</u>", $txt);

$txt = str_replace("", "<marquee>", $txt);

$txt = str_replace("", "</marquee>", $txt);

$txt = str_replace('[url=', '<a href="', $txt);

$txt = str_replace('[/url]', '</a>', $txt);

$txt = str_replace("[hr]", "<hr>", $txt);

$txt = str_replace("", "<img border=0 src=", $txt);

$txt = str_replace("", ">", $txt);

$txt = str_replace("", "<img border=0 src=", $txt);

$txt = str_replace("", ">", $txt);

$txt = str_replace("
", "<div id=quote>quote:<br>", $txt);

$txt = str_replace("", "</div>", $txt);

$txt = str_replace("", "<font color=red>", $txt);

$txt = str_replace("", "</font>", $txt);

$txt = str_replace("", "<font color=blue>", $txt);

$txt = str_replace("", "</font>", $txt);

$txt = str_replace("", "<font color=green>", $txt);

$txt = str_replace("", "</font>", $txt);

$txt = str_replace("", "<font color=orange>", $txt);

$txt = str_replace("", "</font>", $txt);

$txt = str_replace("", "<font color=white>", $txt);

$txt = str_replace("", "</font>", $txt);

$txt = str_replace("", "<font color=black>", $txt);

$txt = str_replace("", "</font>", $txt);

$txt = str_replace("", "<body bgcolor=white>", $txt);
$txt = str_replace("", "</body>", $txt);

$txt = str_replace("
", "<div id=code>", $txt);

$txt = str_replace("", "</div>", $txt);

$txt = str_replace(array("\r\n", "\n", "\r"), '<br>', $txt);

$txt = str_replace("[b]", "<b>", $txt);

$txt = str_replace("[h1]", "<h1>", $txt);

$txt = str_replace("[h2]", "<h2>", $txt);

$txt = str_replace("[h3]", "<h3>", $txt);

$txt = str_replace("[h3]", "<h3>", $txt);

$txt = str_replace("[h4]", "<h4>", $txt);

$txt = str_replace("[h5]", "<h5>", $txt);

return $txt;


function rep2($txt) {

$txt = str_replace('&lt;','<',$txt);

$txt = str_replace('&gt;','>',$txt);

$txt = str_replace('&quot;','\"',$txt);

$txt = str_replace('&#amp;','&',$txt);

return $txt;





09-08-2009, 02:06 AM
How do you want them to embed the video?Can't they just copy and paste the embed code from youtube it into their profile as it is.

09-08-2009, 04:35 AM
Thanks for your reply,

The code I beleived has protection from cross-site scripting? So its basically coded through where it must be BB Codes.

Any thoughts?

09-08-2009, 08:49 AM
Any thoughts?

all you need to do is parse the embed link or embed html provided by the youtube or any other video sharing website, and then through simple input field add details to where details are needed

09-08-2009, 04:27 PM
I understand what you mean, however I do not have enoguh experience to do this.

Could someone help?

09-09-2009, 12:32 AM
Hmm you could add a YouTube button then, which asks for the video ID of the video and then parse it somehow..

You need the code for where the buttons are created also.

1. So basically whent the button is clicked, an alert can popup (javascript) and it can ask for a video id or src you decide..

2. Then this can input something like
user input goes here into their text area

3. Then using the code above you can use a regular expression or something to get the data between the two [youtube] tags and then replace it with the youtube embed code.

4.But what would happen if somebody enters an incorrect video id or src? a blank video would show up, is this acceptable you decide. I can't think of anyway to check if a youtube video src/url is legit really other than maybe to connect to their api or something :)

09-09-2009, 01:11 AM
Here try this:

1. Add the following code below the line: $txt = str_replace("[h5]", "<h5>", $txt); in that code you pasted above.

//Get String Between the 2 tags
$pos_s = strpos($txt,"");
$pos_e = strpos($txt,"");
for($i=$pos_s+strlen("");(($i<($pos_e))&& $i < strlen($txt)); $i++){
$id .= $txt[$i];

//Create the YT Embed Code
$embedCode = <<<EMBED
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/$id&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$id&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>

//Now replace the yt tags with the embed code
$strReplace = "[youtube]".$id."";
$txt = str_replace($strReplace, "$embedCode", $txt);

Then try adding something like
ZcZONRjxpSs in their profile.
Then if this works all you need to do is sort out the button part.

09-09-2009, 02:03 AM
Thank you very much mate this works perfectly!!

Really glad you helped mate thankyou very much.

09-09-2009, 03:01 AM
..Suppose they only put one tag. You need to use regex my friend, it'll make this script much more efficient.

Like zis:

$Arr = array(

$Rep = array(
"<blockquote>$1 <b>Said</b> <br /> $2</blockquote>",
"<a href=\"$1\" target=\"_blank\">$2</a>"

$sText = preg_replace ($Arr, $Rep, $sText);