Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Boag

Boag Avatar
Yo Yo Ma!

**
Official Member

65


September 2005
This is something which I get asked quite often, how to make it possible for a user to use UBB tags to format their text, this is a function which I wrote ages ago, but still use whenever I want UBB to be enabled.

<?php
function ubb_tags($string){
while(preg_match("/\[color=(\w+)\](.+?)\[\/color\]/i",$string)){
$string = preg_replace("/\[color=(\w+)\](.+?)\[\/color\]/i","<font color=$1>$2</font>",$string);
}
while(preg_match("/\[colour=(\w+)\](.+?)\[\/colour\]/i",$string)){
$string = preg_replace("/\[colour=(\w+)\](.+?)\[\/colour\]/i","<font color=$1>$2</font>",$string);
}
while(preg_match("/\[b\](.+?)\[\/b\]/i",$string)){
$string = preg_replace("/\[b\](.+?)\[\/b\]/i","<b>$1</b>",$string);
}
while(preg_match("/\[url=(\w+)\](.+?)\[\/url\]/i",$string)){
$string = preg_replace("/\[url=(\w+)\](.+?)\[\/url\]/i","<a href='$1'>$2</a>",$string);
}
while(preg_match("/\[i\](.+?)\[\/i\]/i",$string)){
$string = preg_replace("/\[i\](.+?)\[\/i\]/i","<i>$1</i>",$string);
}
while(preg_match("/\[u\](.+?)\[\/u\]/i",$string)){
$string = preg_replace("/\[u\](.+?)\[\/u\]/i","<u>$1</u>",$string);
}
while(preg_match("/\[img\](.+?)\[\/img\]/i",$string)){
$string = preg_replace("/\[img\](.+?)\[\/img\]/i","<img src='$1'>",$string);
}
while(preg_match("/\[url\](.+?)\[\/url\]/i",$string)){
$string = preg_replace("/\[url\](.+?)\[\/url\]/i","<a href='$1'>$1</a>",$string);
}
while(preg_match("/\[center\](.+?)\[\/center\]/i",$string)){
$string = preg_replace("/\[center\](.+?)\[\/center\]/i","<center>$1</center>",$string);
}
return $string;
}


I've done a lot of experimentation with making my own UBB tags, which is pretty entertaining, but those are just the usual. To enable UBB tags, put that function round the information which you want the tags to apply to. For example if the text is stored in $post, then you would use the line

$post = ubb_tags($post);

so that the ubb tags would be applied when you print $post further down the page.

- Boag



Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Eh, I use a similar function in my scripts, but that's a bit more complex. :P

Team Studio

Team Studio Avatar

******
Administrator

120


June 2005
O_o
I'll need that script to code my blog...
:D great help indeed!

peter
Guest
I've coded my own parser for my website which is very efficient, and doesn't use any loops (while, for etc).

I wouldn't suggest using a while loop like Boag's parser.

A good parser can be wrote without doing any preg_replace()'s.

:)

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
I'd just use pre_replace, and assuming PHP is like JS, I can use the global attribute with a non-greey replace.

Singular

Singular Avatar
v4 Studios :: Coming Soon

***
Dedicated Member

238


September 2005
So, this doesn't work for me. UBBC still looks like text and HTML still works. :-/
Further Solutions :: Hosting :: Scripts :: Templates
furthersolutions.com

v4 Studios - Coming soon

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
I can try and help, but I need your script.

Singular

Singular Avatar
v4 Studios :: Coming Soon

***
Dedicated Member

238


September 2005
Ok, i will in a few minutes... (I will edit it in here)

<?php
function ubb_tags($string){
while(preg_match("/\(.+?)\[\/color\]/i",$string)){
$string = preg_replace("/\(.+?)\[\/color\]/i","<font color=$1>$2</font>",$string);
}
while(preg_match("/\(.+?)\[\/colour\]/i",$string)){
$string = preg_replace("/\(.+?)\[\/colour\]/i","<font color=$1>$2</font>",$string);
}
while(preg_match("/\[b\](.+?)\[\/b\]/i",$string)){
$string = preg_replace("/\[b\](.+?)\[\/b\]/i","<b>$1</b>",$string);
}
while(preg_match("/\(.+?)\[\/url\]/i",$string)){
$string = preg_replace("/\
(.+?)\[\/url\]/i","<a href='$1'>$2</a>",$string);
}
while(preg_match("/\[i\](.+?)\[\/i\]/i",$string)){
$string = preg_replace("/\[i\](.+?)\[\/i\]/i","<i>$1</i>",$string);
}
while(preg_match("/\[u\](.+?)\[\/u\]/i",$string)){
$string = preg_replace("/\[u\](.+?)\[\/u\]/i","<u>$1</u>",$string);
}
while(preg_match("/\[img\](.+?)\[\/img\]/i",$string)){
$string = preg_replace("/\[img\](.+?)\[\/img\]/i","<img src='$1'>",$string);
}
while(preg_match("/\[url\](.+?)\[\/url\]/i",$string)){
$string = preg_replace("/\[url\](.+?)\[\/url\]/i","<a href='$1'>$1</a>",$string);
}
while(preg_match("/\[center\](.+?)\[\/center\]/i",$string)){
$string = preg_replace("/\[center\](.+?)\[\/center\]/i","<center>$1</center>",$string);
}
return $string;
}
mysql_connect("24.218.236.105:3306","AlienAdmin","PASSWORD");
mysql_select_db('Alien');
echo "<form action='http://home.ripway.com/2005-8/383288/Alien-Files/shoutbox.php' method='post'>
Name:<br><input type='text' name='name' size='20'><br/>
Comment:<br><textarea name='comment' rows='3'></textarea><br/>
<input type='submit' name='addshout' value='Post!'></form><hr width='60' align='left'>";
if(isset($_POST["addshout"])){
$queryye = "INSERT INTO `shouts` (`shoutid`,`name`,`comment`) VALUES ('','".$_POST["name"]."','".$_POST["comment"]."');";
$rrrr = mysql_query($queryye);
echo "Your shout has been posted<br/><br/>Thank you for posting here.<hr width='60' align='left'>";
}
$queryyry = "SELECT * FROM `shouts` ORDER BY `shoutid` DESC";
$rrre = mysql_query($queryyry);
while($rs = mysql_fetch_array($rrre)){
echo "<div style='background-color:#ffffff; overflow:auto;' hieght='90px'>Name: ".$rs["name"]."<br/>Post: ".$rs["comment"]."<br/><hr width='60' align='left'></div>";
}
?>

Preview:
home.ripway.com/2005-8/383288/Alien-Files/shoutbox.php



Last Edit: Oct 19, 2005 0:44:39 GMT by Singular
Further Solutions :: Hosting :: Scripts :: Templates
furthersolutions.com

v4 Studios - Coming soon

Singular

Singular Avatar
v4 Studios :: Coming Soon

***
Dedicated Member

238


September 2005
Thats my code ^
Further Solutions :: Hosting :: Scripts :: Templates
furthersolutions.com

v4 Studios - Coming soon

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Sorry, forgot you said you were gonna edit. :P

And I don't see what would be wrong.

You'd use it like a JS function.

$comments = ubb_tags($comments);

Singular

Singular Avatar
v4 Studios :: Coming Soon

***
Dedicated Member

238


September 2005
Well it doesn't work? Also, JS function? What for? (Why do i need that?)
Further Solutions :: Hosting :: Scripts :: Templates
furthersolutions.com

v4 Studios - Coming soon

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
ITs used like a function is in JS..... And it should work fine.

Singular

Singular Avatar
v4 Studios :: Coming Soon

***
Dedicated Member

238


September 2005
But do i need the function?
Further Solutions :: Hosting :: Scripts :: Templates
furthersolutions.com

v4 Studios - Coming soon

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Yes.

Boag

Boag Avatar
Yo Yo Ma!

**
Official Member

65


September 2005
Yeah, the code's fine, you just need to do what CD said and actually apply the UBB Tag function to your text.

ie.

$comments = ubb_tags($comments);


newBookmarkLockedFalling