How to import VBcode?

Ok. so, finally, after hours of swimming through Kirupa’s .js files, I already have my Javascript to enter the [-b] [-/b] tags. So, when they click on a B button, it automatically enters the tags.

Now, when I bring it from the database to be displayed in the PHP file. I see the [-b] tags, like:

[-b] hey! im supposed to be bolded, but i have this B’s instead [-b]

So, how to I make BBcode work?

Please forgive my ignorance. All I have learned it’s through online tutorials, reading books, or through you guys.

I have been reading the preg_replace and the eregi_replace functions in the PHP.net site. Have been trying some, but can’t get it. Can someone help me replace all the:

[-b] [-/b]
to
<b></b>

and the other tags too?

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

just repeat for the other tags…

I have this:

$string = “hello world”;
$var = str_replace("**", “<b>”, $string);
echo $var;

Why doesn’t it work?

because there isn’t a [b****] in your string…

try this::

$string = "My name is [****b]Bob**";
$string = str_replace("[****b]", "<b>", $string);
echo $string;

str_replace searches a string for the first argument, and replaces it with the second argument…

*Originally posted by Vash *
**because there isn’t a [b****] in your string…

try this::

$string = "My name is [****b]Bob**";
$string = str_replace("[****b]", "<b>", $string);
echo $string;

str_replace searches a string for the first argument, and replaces it with the second argument… **

You have to close the tag as well…

$string = "My name is [****b]Bob**";
$string = str_replace("[****b]", "<b>", $string);
$string = str_replace("[/****b]", "</b>", $string)
echo $string;

lol good point :thumb:

:wink:

sorry. i did had the [-b], thats why the text its bold. sorry :smiley:

I have this form, and people could enter all kinds of formating in it: like lists, bold, change font, change font size, and i’ll have to do one replace of each.

is there a way i can simplify this? or like put the whole replace script in a file and then called it from the page?:puzzle:

*Originally posted by imagined *
**sorry. i did had the [-b], thats why the text its bold. sorry :smiley:

I have this form, and people could enter all kinds of formating in it: like lists, bold, change font, change font size, and i’ll have to do one replace of each.

is there a way i can simplify this? or like put the whole replace script in a file and then called it from the page?:puzzle: **

I’m on a Regular Expression based solution which is more complex but should work better :slight_smile:
EDIT:


<?php
$vbcode = array("b"=>"strong", "u"=>"span style=\"text-decoration:underline;\"");
function toVBCode($string, $vbcode){
	foreach($vbcode as $key => $value){
		$string = preg_replace("#\\[$key\\](.*)\\[/$key\\]#", "<$value>\\\\1</$value>", $string);
	}
	return $string;
}
$test = "**this is a test**, of [u]regexp[/u]";
$test = toVBCode($test, $vbcode);
?>

There you go :slight_smile: just put that in a file, change $vbcode, include it, and then call toVBCode($yourstring, $vbcode). Easy as pie :wink:

I have such a problem with regular expressions… for some reason I just can’t understand how to use them…