|
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
|
|
|
|
Eh, I use a similar function in my scripts, but that's a bit more complex.
|
|
|
|
|
I'll need that script to code my blog... 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.
|
|
|
|
I'd just use pre_replace, and assuming PHP is like JS, I can use the global attribute with a non-greey replace.
|
|
|
|
|
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
|
|
|
|
I can try and help, but I need your script.
|
|
|
|
|
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
|
|
|
|
Thats my code ^
|
Further Solutions :: Hosting :: Scripts :: Templates furthersolutions.com
v4 Studios - Coming soon
|
|
|
|
Sorry, forgot you said you were gonna edit. And I don't see what would be wrong. You'd use it like a JS function. $comments = ubb_tags($comments);
|
|
|
|
|
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
|
|
|
|
ITs used like a function is in JS..... And it should work fine.
|
|
|
|
|
But do i need the function?
|
Further Solutions :: Hosting :: Scripts :: Templates furthersolutions.com
v4 Studios - Coming soon
|
|
|
|
Yes.
|
|
|
|
|
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);
|
|
|