Bbcode Help - Making BBCode

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Bbcode Help - Making BBCode

sparkx
OK well I wanted to add some BBCode to my website but I ran into trouble. At fir
st 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.

 

 

 


Reply

Habble
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.

Reply

sparkx
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

Reply

Habble
Well, what you could do is check for an occurence of " in the bbcode, and then tell them that they used an invalid character?

Reply

sparkx
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

Reply

vujsa
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

 

 

 


Reply

pyost
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

Reply

.:Brian:.
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....

Reply

vujsa
QUOTE(.:Brian:. @ Aug 30 2007, 11:30 AM) *
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

Reply

sparkx
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

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. making bbcode - 60.63 hr back. (2)
  2. getting contents of font tags preg match - 88.55 hr back. (1)
  3. "bbcode animated gif" - 103.16 hr back. (1)
  4. bbc code animated gif - 103.22 hr back. (1)
  5. bbcode for animated gif - 131.53 hr back. (1)
  6. bbcode make website - 282.03 hr back. (1)
  7. bbcode animated gif - 318.42 hr back. (1)
  8. add animated .gif in bbcode - 325.06 hr back. (1)
  9. bbcode chatroom?? - 373.08 hr back. (1)
  10. bbcode chat room - 375.97 hr back. (1)
Similar Topics

Keywords : bbcode, making, bbcode

  1. Making A Value In A Textbox Stay
    (4)
  2. Making Something In Mysql Happen Only Once
    (10)
    Hey! I know I am asking alot. But much is happening theese days. Sorry if I disturb with my
    questions. The thing I am trying to do is: Ex. If the user becomes level 2, he should get 5 skill
    points. I can't do this: CODE if($userlevel=5){ mysql_query("UPDATE user SET skillpoints
    =$points+5");} because then it would update everytime the code was loaded. I hope you understand
    what I am trying to do. If not, tell me /smile.gif" style="vertical-align:middle" emoid=":)"
    border="0" alt="smile.gif" /> and i'll try to explain better. Thanks //Feelay....
  3. Making A Link = Mysql_query
    (8)
    Hey! I will try to make this as clear as possible. how can I make the following. I have a list,
    of all members on my site. If I press on a members name(link), I will come to his profile. To come
    to his profile, I need to get out some vaule from the database, but to get out some value from the
    database, I must tell the code, how it should know who the user is (hard to understand?). To do
    that, I must add a mysql_query in the code ( I think), like "SELECT user FROM dbname WHERE
    user=link".. This is just how I think it works. I know it is kinda wrong.. but I don't k....
  4. Making Animated Gifs
    (5)
    Is it possible to make animated images using PHP's GD library? I've done searches, and I
    can't find anything that explains it fully, or doesn't need you to download a special
    program, which obviously, I couldn't use with Astahost. Can anyone help? I'm not aure how
    animated gifs work, I don't know if all the frames are compressed within the .gif file as
    separate images, or whether it's structured another way.....
  5. Making My Album
    problems with rights (3)
    We have to make something in PHP for school, so I decided to make a complete photoalbum. One of the
    things that it can is creating and storing thumbnails, but here is where the problems start. The
    thumbnails have to be stored in a subfolder called 'thumbnails', if this folder doesn't
    exist, my script creates this folder and everything works like it is supposed to be. But it
    doesn't do that the way I want. The folder is made with: CODE mkdir($thumbnail_folder,
    0777); but when I check it via FTP, it is set to 755. Even worse is that I can't acce....
  6. How To Make Chat Room
    help me in making it (1)
    i need the code of making chatroom but plzzz i need it cute chat room /tongue.gif' border='0'
    style='vertical-align:middle' alt='tongue.gif' /> , and it musnt be by java , if you know any
    website that make chatrooms free plz replay with the website /smile.gif' border='0'
    style='vertical-align:middle' alt='smile.gif' /> Who the hell approved this post? This is a
    question - and that too posted in the Howtos & Tutorials. How did this get approved ? Moved to right
    forum. ....

    1. Looking for bbcode, making, bbcode






*SIMILAR VIDEOS*
Searching Video's for bbcode, making, bbcode
advertisement




Bbcode Help - Making BBCode



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE