Welcome to Webmaster Resources 101


Our Network:

Using str_replace to make BB Code

[ About This Author :: Request Reprint :: Print Article :: Tell A Friend ]

Using str_replace to make BB Code

By Tyler Hopfner
http://www.phphat.net

This is a pretty simple one, you just make a couple arrays with the stuff that you want to be your BB Code, then one that will be the replacements of the BB Code, like this:

<font color="green"><?php

$text="<a href="www.tylerhopfner.com" target="_blank">www.tylerhopfner.com</a>";
$bb=array("<a href="","" target="_blank">","</a>");
$re=array("<a href="","">link</a>");

?></font>

You get the point of that, you can add different tags and replacements if you want, the above is just a small example.

Now all you need to do is use the PHP <i>str_replace()</i> function to make the replacement:

<font color="green"><?php

$text="<a href="www.tylerhopfner.com" target="_blank">www.tylerhopfner.com</a>";
$bb=array("<a href="","" target="_blank">","</a>");
$re=array("<a href="","">link</a>");
$final=str_replace($bb,$re,$text); // will output <a href="www.tylerhopfner.com">link</a>

?></font>

Thats all there is to making your own BB Code.

 
Other Related Material:

Discuss this in our Webmaster Forums

About The Author:
Tyler Hopfner
http://www.phphat.net
Tyler is a fourteen year old guy in grade 9. He have been programming and designing for 2 years now. He is very good at PHP & MySQL, and Photoshop.
Go To Top

This article has been read 1129 times.

[ About This Author :: Request Reprint :: Print Article :: Tell A Friend ]