Jump to content

[SOLVED] BBCode help? =)


Twister1004

Recommended Posts

Hey everyone! I have another question, still with my PHP skills still in development =)

 

Objective:

I am trying to make a BBCode for a webpage so it will just register it when it sees it. More like Using a Smiley and not the "=P" stuff. This is not for forums. This is for like a little comments on a users profile.

 

Issue:

I don't know exactly how to go about this. I was told I was doing it wrong, which now I believe I am, however, I don't know of another way. Either I'm too stupid or I've just never used it. (I've not done anything like this!!!!!)

 

So without further due, here is the original code I wrote,

<?php
error_reporting(E_ALL);
if("[b]" || "[b]"){
echo "<b>";
} elseif("[/b]" || "[/b]"){
echo "</b>";
} elseif("[i]" || "[i]"){
echo "<i>";
} elseif("[/i]" || "[/i]"){
echo "</i>";
} elseif("[u]" || "[u]"){
echo "<u>";
} elseif("[/u]" || "[/u]"){
echo "</u>";
} elseif("
[center]"){
echo "<center>";
} elseif("[/center]
"){
echo "</center>";
} elseif("
[right]"){
echo "<span align=\"right\">";
} elseif("[/right]
"){
echo "</span>";
} elseif("
[left]"){
echo "<span align=\"left\">";
} elseif("[/left]
"){
echo "</span>";
} elseif("[img]http://"){
echo "<img src=\"";
} elseif("[/img]"){
echo "\" alt=\"Image\"";
} elseif("[url=http://"){
echo "<a href=\"$\">";
} elseif("]"){
echo "<a href=\"$\">";
} elseif("[/url]"){
echo "</a>";
} elseif("[font]"){
echo "<font face=\"";
} elseif("[/font]"){
echo "\"</font>";
} elseif("[color]"){
echo "<font color=\"";
} elseif("[/color]"){
echo "\"</font>";
} elseif("" || "=)"){
echo "<img src=\"parser/smileys/smile.gif\" alt=\"sad\"";
} elseif("" || "=P"){
echo "<img src=\"parser/smileys/tounge.gif\" alt=\"sad\"";
} elseif(":/" || "=/"){
echo "<img src=\"parser/smileys/bored.gif\" alt=\"sad\"";
} elseif(""){
echo "<img src=\"parser/smileys/lol.gif\" alt=\"sad\"";
} elseif("" || ">=("){
echo "<img src=\"parser/smileys/mad.gif\" alt=\"sad\"";
} elseif("" || "=O"){
echo "<img src=\"parser/smileys/smile.gif\" alt=\"sad\"";
} elseif("" || "=("){
echo "<img src=\"parser/smileys/sad.gif\" alt=\"sad\"";
} elseif(":wink:" || "^_~" || "" || ""){
echo "<img src=\"parser/smileys/wink.gif\" alt=\"wink\"";
}
?>

 

 

Any help would be greatly appreciated!

Link to comment
Share on other sites

Don't take this the wrong way, but you're doing that entirely wrongly.

 

 

 

When ever you do if(condition), the yes branch of the if tree only executes if the "condition" evaluates to true.

 

 

So essentially, you're doing:

 

if("[b]" == true) {
    echo '<b>';
}

 

 

You need to be replacing the codes inside of strings though.

 

 

http://php.net/str_replace.

 

 

For example, to replace [ b] (ignore the space) with <b>, you could do:

 

$content = "Look at the [b]bold word.[/b]";
$content = str_replace(array('[b]', '[/b]'), array('<b>', '</b>'), $content);

 

 

Or, you could go the regexp route (which you will probably want to avoid unless you have to use if you're new to PHP.

 

$content = "Look at the [b]bold word.[/b]";
$content = preg_replace('~[b](.*?)[/b]~', '<b>\1</b>', $content);

Link to comment
Share on other sites

No offense taken Corbin. I'm not easily offended when I'm trying to learn something new.

 

So, by how this is setup (Really confuzzling to me, but still learning its structor), there is a loop going on, and it is named $c.

 

So to do what your saying I need to do, but I think I get it, please correct me if I'm wrong,

 

$code = str_replace("[-b]", "<b>", "bold");

 

The first one would be if it finds it ([-b]) then it will replace it with <b>. Then the "bold" is its name?

Link to comment
Share on other sites

str_replace works like this:

 

 

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

 

 

 

The first param is what to replace, the second param is what to replace it with, and the third param is the input.

 

 

So, for example:

 

echo str_replace("world", "Twister1004", "Hello world!");

 

 

Would output "Hello Twister1004!"

Link to comment
Share on other sites

OMG CORBIN I LOVE YOU!!! <3

 

*IS EXCITED~!*

 

I actually get it now =D!!!!

 

str_replace("hey!", "Go die!", "Hey! Tyler!");

 

So let me get this clear and make sure!

 

1st parameter ("hey!") Is what it is looking for.

2nd parameter ("Go Die!") is what it will replace once it finds the "hey!".

3rd parameter ("Hey! Tyler!") is the input and will look in this parameter and look for the "hey!" and then it will replace it?

 

 

Final Clear up, This is my final answer for $50,000!!! IS IT CORRECT?! *Waits for the answer!*

Link to comment
Share on other sites

ok,

 

Well, I just wrote a few codes, but they doesn't wanna work.

 

Any ideas what I did wrongly? xD

 

str_replace(array("[b]", "[/b]"), array("<b>", "</b>"), $c['comment']);
str_replace(array("[i]", "[/i]"), array("<i>", "</i>"), $c['comment']);
str_replace(array("[u]", "[/u]"), array("<u>", "</u>"), $c['comment']);
str_replace(array("
[center]", "[/center]
"), array("<center>", "</center>"), $c['comment']);
str_replace(array("
[right]", "[/right]
"), array("<span align=\"right\">", "</span>"), $c['comment']);
str_replace(array("
[left]", "[/left]
"), array("<span align=\"left\">", "</span>"), $c['comment']);
str_replace(array("[img=http://", "]"), array("<img src = \" \">", "</img>"), $c['comment']);
str_replace(array("[url=http://", "]", "[/url]"), array("<a href=\"\">","</a>"), $c['comment']);
str_replace(array("[color]", "[/color]"), array("<font color=\"\">", "</font>"), $c['comment']);
str_replace(array("[font]", "[/font]"), array("<font face=\"\">", "</font>"), $c['comment']);

 

 

Link to comment
Share on other sites

Here is a complete bbcode engine.

 

bbcode_engine.php

<?php
/*
|------------------------------------------------|
|  BBcode Parsing                                |
| Core engine for MSNForums 1.0                  |
| Copyright 2005 Stewart Marshall                |
|------------------------------------------------|
*/
class bbcode_engine {
var $parsings=array();
var $htmls=array();
function simple_bbcode_tag($tag="")
{

if (! $tag)
{
break;
}
$this->parsings[]="/\[".$tag."\](.+?)\[\/".$tag."\]/";
$this->htmls[]="<".$tag.">\\1</".$tag.">";
}
function adv_bbcode_tag($tag="",$reptag="")
{

if (!$tag)
{
break;
}

$this->parsings[]="/\[".$tag."\](.+?)\[\/".$tag."\]/";
$this->htmls[]="<".$reptag.">\\1</".$reptag.">";
}

function simple_option_tag($tag="",$optionval="")
{
      
if ($tag=="" || $optionval=="")
{
break;
}
$this->parsings[]="/\[".$tag."=(.+?)\](.+?)\[\/".$tag."\]/";
$this->htmls[]="<".$tag." ".$optionval."='\\1'>\\2</".$tag.">";
}

function adv_option_tag($tag="",$reptag="",$optionval="")
{
      
if ($tag=="" || $optionval=="" || $reptag=="")
{
break;
}
$this->parsings[]="/\[".$tag."=(.+?)\](.+?)\[\/".$tag."\]/";
$this->htmls[]="<".$reptag." ".$optionval."='\\1'>\\2</".$reptag.">";
}
function adv_option_tag_em($tag="",$reptag="",$optionval="")
{
      
if ($tag=="" || $optionval=="" || $reptag=="")
{
break;
}
$this->parsings[]="/\[".$tag."=(.+?)\](.+?)\[\/".$tag."\]/";
$this->htmls[]="<".$reptag." ".$optionval."='mailto:\\1'>\\2</".$reptag.">";
}

function simp_option_notext($tag="",$optionval="")
{
      
if ($tag=="" || $optionval=="")
{
break;
}
$this->parsings[]="/\[".$tag."=(.+?)\]/";
$this->htmls[]="<".$tag." ".$optionval."='\\1' />";
}
function adv_option_notext($tag="",$reptag="",$optionval="")
{
      
if ($tag=="" || $optionval=="" || $reptag=="")
{
break;
}
$this->parsings[]="/\[".$tag."=(.+?)\]/";
$this->htmls[]="<".$reptag." ".$optionval."='\\1' />";
}
function adv_option_notext_em($tag="",$reptag="",$optionval="")
{
      
if ($tag=="" || $optionval=="" || $reptag=="")
{
break;
}
$this->parsings[]="/\[".$tag."=(.+?)\]/";
$this->htmls[]="<".$reptag." ".$optionval."='mailto:\\1' >\\1</".$reptag.">";
}

function simp_bbcode_att($tag="",$optionval="")
{

if ($tag=="" || $optionval=="")
{
break;
}
$this->parsings[]="/\[".$tag."\](.+?)\[\/".$tag."\]/";
$this->htmls[]="<".$tag." ".$optionval."='\\1' />";
}
function adv_bbcode_att($tag="",$reptag="",$optionval="")
{

if ($tag=="" || $optionval=="" || $reptag=="")
{
break;
}
$this->parsings[]="/\[".$tag."\](.+?)\[\/".$tag."\]/";
$this->htmls[]="<".$reptag." ".$optionval."='\\1' />";
}
function adv_bbcode_att_em($tag="",$reptag="",$optionval="")
{

if ($tag=="" || $optionval=="" || $reptag=="")
{
break;
}
$this->parsings[]="/\[".$tag."\](.+?)\[\/".$tag."\]/";
$this->htmls[]="<".$reptag." ".$optionval."='mailto:\\1'>\\1</".$reptag.">";
}


function cust_tag($bbcode="",$html="")
{

if ($bbcode == "" || $html == "")
{
break;
}
$this->parsings[]=$bbcode;
$this->htmls[]=$html;
}

function parse_bbcode($text)
{

$i=0;
while($this->parsings[$i])
{

$text=preg_replace($this->parsings[$i],$this->htmls[$i],$text);
$i++;
}		
return $text;
}
function export_parsings()
{
return $this->parsings;
}
function export_htmls()
{
return $this->htmls;
}
}

class bbcode {
var $engine="";
function bbcode()
{
require "bbcode_engine.php";
$this->engine= new bbcode_engine;
$this->engine->cust_tag("/</","<");
$this->engine->cust_tag("/>/",">");
//Since \n and <br> screw up preg, convert them out.
$this->engine->cust_tag("/\n/","&nbrlb;");
$this->engine->simple_bbcode_tag("b");
$this->engine->simple_bbcode_tag("i");  
$this->engine->simple_bbcode_tag("u");  
$this->engine->simple_bbcode_tag("s");
$this->engine->simple_bbcode_tag("sub");  
$this->engine->simple_bbcode_tag("sup");
$this->engine->simple_bbcode_tag("big");
$this->engine->simple_bbcode_tag("small"); 
$this->engine->adv_bbcode_tag("list","ul");
$this->engine->adv_bbcode_tag("olist","ol");
$this->engine->adv_bbcode_tag("item","li");
$this->engine->adv_option_tag("font","font","family");
$this->engine->adv_option_tag("size","font","size"); 
$this->engine->adv_option_tag("url","a","href");
$this->engine->adv_option_tag("color","font","color");
$this->engine->adv_option_tag("style","span","style");
$this->engine->simp_option_notext("img","src");
$this->engine->simp_bbcode_att("img","src");
$this->engine->cust_tag("/\(c\)/","©");
$this->engine->cust_tag("/\(tm\)/","&#38;#153;");
$this->engine->cust_tag("/\(r\)/","®");
$this->engine->adv_option_tag_em("email","a","href");
$this->engine->adv_bbcode_att_em("email","a","href");
$this->engine->cust_tag("/\[left\](.+?)\[\/left\]/","<div align='left'>\\1</div>");
$this->engine->cust_tag("/\[center\](.+?)\[\/center\]/","<div align='center'>\\1</div>");
$this->engine->cust_tag("/\[right\](.+?)\[\/right\]/","<div align='right'>\\1</div>");
$this->engine->cust_tag("/\[quote=(.+?)\](.+?)\[\/quote\]/","<div class='quotetop'>QUOTE(\\1)</div><div class='quotemain'>\\2</div>");
$this->engine->cust_tag("/\[quote\](.+?)\[\/quote\]/","<div class='quotetop'>QUOTE</div><div class='quotemain'>\\1</div>");
$this->engine->cust_tag("/\[code\](.+?)\[\/code\]/","<div class='codetop'>CODE</div><div class='codemain'><code>\\1</code></div>");
$this->engine->cust_tag("/\[codebox\](.+?)\[\/codebox\]/","<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>\\1</div>");
$this->engine->cust_tag("/&nbrlb;/","<br />\n"); 
}
function bbcode_parse($html)
{
return $this->engine->parse_bbcode($html);
}
}
$bbc = new bbcode;
?>

 

Now on every page you going to use bbcode. Put:

 

include('bbcode_engine.php');

 

at the top of your page.

 

Then to display this:

 

$bbc->bbcode_parse($output);

 

now put this above the above code:

 

$output = '[b]Look im bold?[/b]
[i]Im italicalized[/i]

[center]Centered?[/center]

[u]Not centered but underlined[/u]';

 

All the spaces are already included and there for you do not need to add <br>.

 

Enjoy ;)

Link to comment
Share on other sites

If you're going to make a full out BBCode system, you should stop now, learn some basic regular expressions and then restart.

 

This one is easy with regular expressions:

 

$content = preg_replace('~\{1,6})|black|red|green|blue|purple|yellow)](.*?)\[\/color\]~', '<font style="color: \1;">\2</font>');

 

But without regular expressions....  You would have to find all instances of [-color in the text, then capture the part after the =, then find the part in between [-color] and [ /color]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.