# How to import VBcode?

**URL:** https://forum.kirupa.com/t/how-to-import-vbcode/45172
**Category:** Uncategorized
**Created:** [March 11, 2004, 5:16pm UTC](https://forum.kirupa.com/t/how-to-import-vbcode/45172 "2004-03-11T17:16:28Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)
#### Post date: [March 11, 2004, 5:16pm UTC](https://forum.kirupa.com/t/how-to-import-vbcode/45172/1 "2004-03-11T17:16:28Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 11, 2004, 6:44pm UTC](https://forum.kirupa.com/t/how-to-import-vbcode/45172/2 "2004-03-11T18:44:15Z")

</div>

I have been reading the preg\_replace and the eregi\_replace functions in the [PHP.net](http://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?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 11, 2004, 6:46pm UTC](https://forum.kirupa.com/t/how-to-import-vbcode/45172/3 "2004-03-11T18:46:02Z")

</div>

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

```

just repeat for the other tags…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 11, 2004, 6:54pm UTC](https://forum.kirupa.com/t/how-to-import-vbcode/45172/4 "2004-03-11T18:54:46Z")

</div>

I have this:

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

Why doesn’t it work?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 11, 2004, 7:01pm UTC](https://forum.kirupa.com/t/how-to-import-vbcode/45172/5 "2004-03-11T19:01:22Z")

</div>

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

try this::

```php
$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…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 11, 2004, 7:03pm UTC](https://forum.kirupa.com/t/how-to-import-vbcode/45172/6 "2004-03-11T19:03:52Z")

</div>

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

try this::

```php
$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…

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

```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 11, 2004, 7:08pm UTC](https://forum.kirupa.com/t/how-to-import-vbcode/45172/7 "2004-03-11T19:08:27Z")

</div>

lol good point :thumb:

😉

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 11, 2004, 7:29pm UTC](https://forum.kirupa.com/t/how-to-import-vbcode/45172/8 "2004-03-11T19:29:26Z")

</div>

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

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:

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 11, 2004, 7:31pm UTC](https://forum.kirupa.com/t/how-to-import-vbcode/45172/9 "2004-03-11T19:31:15Z")

</div>

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

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 🙂  
EDIT:

```php

<?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 🙂 just put that in a file, change $vbcode, include it, and then call toVBCode($yourstring, $vbcode). Easy as pie 😉

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 11, 2004, 7:53pm UTC](https://forum.kirupa.com/t/how-to-import-vbcode/45172/10 "2004-03-11T19:53:23Z")

</div>

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