FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


bb code help - regex





clydejaw
Hey guys!

I'm currently working on building a small forum that takes bb codes.

It's capturing everything inbetween [ code ] [ /code ] like:

Code:
this


At the moment it's coming together nicely, however I have another method that shortends anything that is a link, http://website.com/blah/blah and is displayed as website.com rather than displaying as http://website.com/blah/blah. This is so when people post long links, it won't use up too much character space.

However, my problem is that if I use [ code ] <a href="http://website.com"> [ /code ] it becomes

Code:
<a href="website.com


So when I post anything in between code, it applies that method which is currently conflicting. It should be like this:

Code:
<a href="http://website.com/blah/blah">



Say I have a string

"hello, this is my code [ code ] <a href="http://website.com/blah" [ /code ] but this is a link outside the code http://website.com"

My current logic is find anything in that string that contains a [ code ] [ /code ] and if it exists, don't apply the shortend_link method. However my desired result was suppose to be:

hello, this is my code
Code:
 <a href="http://website.com/blah"
but this is a link outside the code website.com

Because any links outside [ code ] [ /code ] should be shortended and only links in between code should be the full path.

I hope this is not too confusing Neutral but I need a better logic around this. I was thinking of breaking each part in to an array:

[0] hello, this is my code
[1] [ code ] <a href="http://website.com/blah" [ /code ]
[2] but this is a link outside the code http://website.com

and only apply the shortend_link method on [2] but it's doing my head in. Is there a way to use preg method to break them in to an array like that and then reconstruct the string again?

Thanks in advance guys!!
jmraker
Have you tried looking at the source of other bb code implementations to see what they do?
http://www.phpbb.com/downloads/olympus.php
adsmail27
You can find enough on google alternatives
ogah
tray use [ code ] <a href=http://website.com> [ /code ] (without quote)
if without qoute work fine maybe you need replace quote with htmspecialchars in your bbcode script
sonam
Actually I don't understand your question right but I think you can use if else statement for find what your scrip must short and what not. For example (I am put two different string in an array):

Code:
$strings =  array('[ code ]<a href="http://website.com/blah/blah/" >http://website.com/blah/blah/</a>[ /code ]', '<a href="http://website.com/blah/blah/" >http://website.com/blah/blah/</a>');
foreach($strings as $string) {
   if(strpos($string, '[ /code ]') !== FALSE) {
       echo $string . '<br />';
   } else {
       $str1 = explode(">", $string);
       $str2 = explode("//", $str1[1]);
       $str3 = explode("/", $str2[1]);
       $short_string = $str1[0] . '>http://' . $str3[0] . '/</a><br />';
       echo $short_string;
   }
}



Sonam
ogah
hmm.. i think i know what wrong with your bbcode script.
replace "<" and ">" with htmlentaties before processing to bbcode
eg.
if your script looks like this
Code:
$msg = $_POST['msg'];
$msg = bbcode($msg);


you must edit like this
Code:
$msg = $_POST['msg'];
$msg = str_replace('<', '&lt;', $msg);
$msg = str_replace('>', '&gt;', $msg);
$msg = bbcode($msg);
Related topics
a game online
Help with PhpBB
C++ code help
XHTML code help
What's Wrong with Making Money?
BB Codes
BB Colours
BB Codes
BB Code :|
uncoded code ! :P
need help with bbcode.....
BB Code [EDIT: Offering 150 FRIH$]
How to realize "BB-Code"
Trojan-Clicker HTML IFrame.gv" removal from code help.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.