Setup a new php document and make a function called BBCode
CODE
<?php
function BBCode ($string) {
}
?>
function BBCode ($string) {
}
?>
Then we need to use regular expressions and arrays for the search and replace
CODE
<?php
function BBCode ($string) {
$search = array(
'\[b\](.*?)\[\/b\]\',
'\[i\](.*?)\[\/i\]\',
'\[u\](.*?)\[\/u\]\',
'\[img\](.*?)\[\/img\]\',
'\[url\=(.*?)\](.*?)\[\/url\]\',
'\[code\](.*?)\[\/code\]\'
);
$replace = array(
'<b>\\1</b>',
'<i>\\1</i>',
'<u>\\1</u>',
'<img src="\\1">',
'<a href="\\1">\\2</a>',
'<code>\\1</code>'
);
return preg_replace($search, $replace, $string);
}
?>
function BBCode ($string) {
$search = array(
'\[b\](.*?)\[\/b\]\',
'\[i\](.*?)\[\/i\]\',
'\[u\](.*?)\[\/u\]\',
'\[img\](.*?)\[\/img\]\',
'\[url\=(.*?)\](.*?)\[\/url\]\',
'\[code\](.*?)\[\/code\]\'
);
$replace = array(
'<b>\\1</b>',
'<i>\\1</i>',
'<u>\\1</u>',
'<img src="\\1">',
'<a href="\\1">\\2</a>',
'<code>\\1</code>'
);
return preg_replace($search, $replace, $string);
}
?>
That runs a search through a specified string, it tells it to look for b, i, u, img, url, and code.
It then turns it into html format.
Usuage of this function is
CODE
<?php
echo BBCode('Here is some [b]Bold![/b] and so on!');
?>
echo BBCode('Here is some [b]Bold![/b] and so on!');
?>
Enjoi.