Welcome Guest ( Log In | Register )




                Web Hosting Guide

 
Reply to this topicNew Topic
Bbcode Help, Making BBCode
sparkx
post Aug 20 2007, 03:24 PM
Post #1


Sparkx
Group Icon

Group: [HOSTED]
Posts: 376
Joined: 11-October 06
From: Dana Point, CA, USA
Member No.: 16,496
myCENTs:55.07


OK well I wanted to add some BBCode to my website but I ran into trouble. At first I was useing str_replace() but that does not work if I am trying to make the   code. I think I would use preg_match but I am real confused. Could someone post the source code to make:
CODE
[url=http://somewebsite.com]Website[/url]
  Into:
CODE
<a href='http://somewebsite.com' target='_blank'>Website</a>


Also how would I make the code tag so it would replace all [ and ] withen the two strings with &#91 and &#93 example
CODE
[cod][/cod]
would print the html
CODE
&#91b&#93 &#91/b&#93

Do you get what I am trying to do?
Thanks,
Sparkx
Note: I had to take out some letters in the code above because this forum looked at them wrong.

This post has been edited by sparkx: Aug 20 2007, 03:27 PM
Go to the top of the page
 
+Quote Post
Habble
post Aug 21 2007, 06:45 AM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 286
Joined: 17-June 07
From: Tasmania
Member No.: 22,699


Well, for the url one, I think you could do this: (Where $string is the text you're converting to bbcode)
CODE
$stringarray = explode("[url=", $string);
$stringarray = explode("]", $stringarray[1]);
$url = $stringarray[0];
//We have the URL
$stringarray2 = explode("[url=".$url."]", $string);
$stringarray2 = explode("[/url]", $stringarray2[1]);
$text = $stringarray2[0];
//We have the text
$string = str_replace("[url=".$url."]".$text."[/url]", '<a href="'.$url.'">'.$text.'</a>', $string);

This gets the text and url from the bbcode, then puts it into an <a> tag.
Go to the top of the page
 
+Quote Post
sparkx
post Aug 22 2007, 05:16 PM
Post #3


Sparkx
Group Icon

Group: [HOSTED]
Posts: 376
Joined: 11-October 06
From: Dana Point, CA, USA
Member No.: 16,496
myCENTs:55.07


Looks like it would work but I dont know much about the explode function. The one thing that I see that worries me a little is that ] is being used sepuratly. The question I have is if ] is used incorrectly will it still be recoginized in this str_replace? Example: [url=http://www.example.php" target="_blank] now the replace would look like: <a href="http://www.example.php" target="_blank"> will this cause the ability to inject stuff into the tag? It could be bad if they used " or > would there be a way to replace all " and > in the $url tag without messing up any possible urls? Also will this work multiple times lets say the tag is used more then once? If the URL tag works I dont see why I couldn't do the same with the [cod][/cod] tag and running a string replace for [ and ] for the section inside. Im sorry if I am asking too many questions but I dont want to end up like PHPBB with security holes everywhere.
Thanks for the help,
Sparkx
Go to the top of the page
 
+Quote Post
Habble
post Aug 23 2007, 06:28 AM
Post #4


Premium Member
Group Icon

Group: [HOSTED]
Posts: 286
Joined: 17-June 07
From: Tasmania
Member No.: 22,699


Well, what you could do is check for an occurence of " in the bbcode, and then tell them that they used an invalid character?
Go to the top of the page
 
+Quote Post
sparkx
post Aug 26 2007, 10:52 PM
Post #5


Sparkx
Group Icon

Group: [HOSTED]
Posts: 376
Joined: 11-October 06
From: Dana Point, CA, USA
Member No.: 16,496
myCENTs:55.07


OK so it is secure apparently. Just wanted to make sure. If you dont mind I have anouther related question.
Well on some browsers you dont neccissarly need " in tags to make them valid. I know for some browsers <font color=#00FF00> ect is a valid tag. The point I was trying to make is if there is a way to check the URL for if it is valid and is not added onto (injection ect)? I know you can get php to check an image for size ect. Is PHP also able to check a url for valid? What I was thinking was if the URL was invalid or was a .exe ect url that downloads something it would be replaced with Invalid URL rather then <a href=... That could be done if the check would produce true then just run a simple if($var==true){ tag. Do you get what I am saying? Check if a URL is .html or .php just like you can check an image for if it is .gif or .jpg ect.
Thanks again for the help. I know this question could be under a new Topic but I dont see why making more topics for related question.
Sparkx
Go to the top of the page
 
+Quote Post
vujsa
post Aug 29 2007, 08:51 AM
Post #6


Absolute Newbie
Group Icon

Group: Admin
Posts: 888
Joined: 20-February 05
From: Indianapolis, Indiana, USA (Midwest)
Member No.: 2,714
myCENTs:35.43


I think the best and most common method is to use regular expressions. Using regular expressions allows you to match a wider variety of patterns. If you only use str_replace, then if the user makes a mistake in the BBC or the BBC contains something you weren't expecting, you could get a lot of errors.

For example, here is some BBC for URL using regular expressions:
CODE
<?php

$input = "[url]http://www.handyphp.com[/url]<br />\n[url=http://www.handyphp.com]Handy PHP[/url]";
$pattern = array(
            '@(\[url=)([^\]]*?)(\])(.*?)(\[/url\])@si', // This matches [url=http://www.domain.com]Domain[/url]
            '@(\[url)([^\]]*?)(\])(.*?)(\[/url\])@si' // This matches [url]http://www.domain.com[/url]
            );
$replace = array(
            '<a href="${2}">${4}</a>',
            '<a href="${4}">${4}</a>'
            );
$output = preg_replace($pattern, $replace, $input);

echo $input . "\n<hr />\n" . $output;
?>

See, instead of replacing a part of the BBC, the entire tag is replaced and selected parts of the tag is reinserted into the new string. The first pattern is actually composed of 5 sub-patterns:
(\[url=) - First, the string must start with "[ur..."
([^\]]*?) - Second, match everything after that up to but not including "]" - This is the "http..."
(\]) - Third, find the end bracket for the opening tag.
(.*?) - Fourth, Match everything here until the next sub-pattern. - This is the "Handy PHP"
(\[/url\]) - Fifth, find the closing tag for the BBC.

Now, the replacements include back references to parts of the original string. For example, ${2} means use the second sub-pattern match from the original string which is http://www.handyphp.com.

Since you are matching a full string instead of pieces and parts of a string, you can better control how the output will be formated. While this pattern doesn't tackle the issue of single, double, or no quotes being used by the user, it could be easily modified to do so.

See this new version that looks for single and double quotes:
CODE
<?php

$input = "[url]http://www.handyphp.com[/url]<br />\n
[url=\"http://www.handyphp.com\"]Handy PHP[/url]<br />\n
[url='http://www.handyphp.com']Handy PHP[/url]<br />\n
[url=http://www.handyphp.com]Handy PHP[/url]";
$pattern = array(
            '@(\[url=)(\'|")*([^\]]*?)(\'|")*(\])(.*?)(\[/url\])@si',
            '@(\[url)([^\]]*?)(\])(.*?)(\[/url\])@si'
            );
$replace = array(
            '<a href="${3}">${6}</a>',
            '<a href="${4}">${4}</a>'
            );
$output = preg_replace($pattern, $replace, $input);

echo $input . "\n<hr />\n" . $output;
?>


You may have noticed that I use arrays for both pattern and replace. preg_replace will cycle through each array item in $pattern and replace it with the corresponding item from $replace. You should also see that I have 2 different patterns and 2 different matches. This is because of the 2 different methods that the URL BBC can usually be implemented. The first should always be the more specific pattern followed by the more general. Since the link with a a name is a more complex string, the pattern for it has to be more specific.

Due to the complexity of regular expressions, many newer programmers have a lot of trouble figuring out how to use them. In fact, I still learn new things every time I try to use regular expressions. I depend a lot on trial and error. The above examples are not quite as optimized as they could be but these are the easiest to understand examples I could come up with. For example, since you only need 2 back references in this example, it isn't really necessary to have everything broken down into sub-patterns. Only that which you want to back reference needs to be sub-patterned.

I recommend that you do more research into regular_expression. Here is a good place to learn: http://www.ilovejackdaniels.com/cheat-shee...ns-cheat-sheet/
In general, that is a fantastic website for web developers. I printed a number of the full color cheat sheets onto glossy double sided photo paper.

As for checking for injections, you can reject urls the end with ".exe" if you want. It just requires you to adjust the regular expression to "NOT" match the BBC if it contains a link with .exe at the end. Or you could replace offensive links with some other string which is easier and will cover all links in the input and not just the once in BBC.

I hope this helps cool.gif
vujsa
Go to the top of the page
 
+Quote Post
pyost
post Aug 29 2007, 05:25 PM
Post #7


Nenad Bozidarevic
Group Icon

Group: [MODERATOR]
Posts: 1,087
Joined: 7-November 05
From: Belgrade, Serbia
Member No.: 9,500
myCENTs:42.34


Great mini-tutorial on regular expression and their use with BBCode. This is very useful when creating web sites with user input and I am sure lots of people will benefit from it wink.gif
Go to the top of the page
 
+Quote Post
.:Brian:.
post Aug 30 2007, 03:30 PM
Post #8


Premium Member
Group Icon

Group: Members
Posts: 219
Joined: 13-February 07
Member No.: 20,371


I would like to present another option for you here...

You could always use a bbcode engine from forum or blogging software if your website has those on them...it would be as simple as including the files necessary to have the functions to parse bbcode (just make sure you don't violate any copyright restrictions there, and that you give proper credit).

But that is what I have done with my website and it seems to work really well for me....
Go to the top of the page
 
+Quote Post
vujsa
post Sep 23 2007, 02:23 PM
Post #9


Absolute Newbie
Group Icon

Group: Admin
Posts: 888
Joined: 20-February 05
From: Indianapolis, Indiana, USA (Midwest)
Member No.: 2,714
myCENTs:35.43


QUOTE(.:Brian:. @ Aug 30 2007, 11:30 AM) [snapback]110143[/snapback]
I would like to present another option for you here...

You could always use a bbcode engine from forum or blogging software if your website has those on them...it would be as simple as including the files necessary to have the functions to parse bbcode (just make sure you don't violate any copyright restrictions there, and that you give proper credit).

But that is what I have done with my website and it seems to work really well for me....

That is an excellent idea Brian. Not only does this give you more time to develop other aspects of your website, it also guarantees that the BBC will be the same on the entire website. Not to mention, if your forum or blog has developed it already, it probably doesn't have any bugs in it.

Come to think of it, I have a section on my website I might try this with. I don't particularly like the BBC used on my forum but at least it would add consitancy to the website if I used it in the other section. Of course, at some point in time, I have to "customize" the BBC in my forum. laugh.gif

vujsa
Go to the top of the page
 
+Quote Post
sparkx
post Oct 28 2007, 01:54 AM
Post #10


Sparkx
Group Icon

Group: [HOSTED]
Posts: 376
Joined: 11-October 06
From: Dana Point, CA, USA
Member No.: 16,496
myCENTs:55.07


Thanks for the tutorial. Sorry for the late reply but at the time I didn't see a big need to reply. I used the preg-replace and I have been working with it a lot. I just has a quick additional question. Is it possible (like above) to set a variable to the BBCode? I would like to do something like the following:
CODE
$var="[bbcode]<test>[/bbcode]";
$var2=preg_replace(<this is the part I need help with>, <and this also>,$var);
$var3=str_replace(array('<','>'), array('<', '>'), $var2);
<then somehow get this statment back into the origonal var where it was replaced from.
echo($var);

I don't know if you can understand that but basicly what I want is to replace < and > with the HTML version of < and > for everything between [bbcode] and [/bbcode]. What I want is just like the [ code ] [/ code ] function on this BB.
Thanks again for all the help and the link,
Sparkx
Go to the top of the page
 
+Quote Post

Reply to this topicNew Topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No new   24 pyost 7,267 4th April 2008 - 03:10 AM
Last post by: iGuest
No New Posts   4 Houdini 1,257 28th May 2006 - 11:40 PM
Last post by: Houdini
No New Posts   1 Kushika 2,246 11th April 2006 - 08:28 PM
Last post by: Hercco
No New Posts   4 miCRoSCoPiC^eaRthLinG 1,203 2nd January 2006 - 07:31 AM
Last post by: mayank


Web Hosting Powered by ComputingHost.com.
HONESTY ROCKS! truth rules.
Creative Commons License