me1000 Posted June 3, 2006 Share Posted June 3, 2006 ok I want to create bb codes,I found this little script. but it doesnt support images.[code] $string = $_CONTENT['TUT_CONTENT'];$bb_replace = array ('/(\[[Bb]\])(.+)(\[\/[Bb]\])/','/(\[url=)(.+)(\])(.+)(\[\/url\])/');$bb_replacements = array ('<b>\\2</b>','<a href="\\2">\\4</a>');$string = preg_replace($bb_replace, $bb_replacements, $string);print $string;[/code]Ive already installed it ant it works fine, (except the image part)[code]$bb_replace = array ('/(\[[Bb]\])(.+)(\[\/[Bb]\])/','/(\[url=)(.+)(\])(.+)(\[\/url\])/','/(\[img])(.+)(\[\/img\]);$bb_replacements = array ('<b>\\2</b>','<a href="\\2">\\4</a>','<img scr="\\2">');[/code]would something like that work?if so, can you please explain why, because I have no idea what those numbers mean!Thanks!edit:also when i enter something in the text field and then hit submit it enters almost everything (in the text field) to the database. Im having touble with line breaks though.say if you hit enter 3 times on this forum, then in the post you would see 3 lines spaces.How do i do the same thing with my text box? Quote Link to comment https://forums.phpfreaks.com/topic/11100-creating-bb-code/ Share on other sites More sharing options...
wildteen88 Posted June 3, 2006 Share Posted June 3, 2006 Change your code to this:[code]$bb_replace = array ('/(\[[Bb]\])(.+)(\[\/[Bb]\])/','/(\[url=)(.+)(\])(.+)(\[\/url\])/','/(\[img\])(.+)(\[\/img\]);$bb_replacements = array ('<b>\\2</b>','<a href="\\2">\\4</a>','<img scr="\\2">');[/code]And about line breaks. The line breaks are being inserted into the database, but the actuall problem is the web browser. Web browsers ignore white spacecharacters, such as newlines, carriage returns etc. To solve this you'll want to use a function called nl2br. Now use this function when you are retrireving data out of of the database like so:$var = nl2br($row['fieldnamehere']);What this will now do is convert newlines/carriage returns into their html equivilant the linebreak (< br />) tag. Quote Link to comment https://forums.phpfreaks.com/topic/11100-creating-bb-code/#findComment-41519 Share on other sites More sharing options...
me1000 Posted June 3, 2006 Author Share Posted June 3, 2006 Thanks for the help, but it is still not working! :Sthis is my code that displays the info[code]<td> <br> <? $string = $tut_withlines;$bb_replace = array ('/(\[[Bb]\])(.+)(\[\/[Bb]\])/','/(\[url=)(.+)(\])(.+)(\[\/url\])/','/(\[img\])(.+)(\[\/img\])');$bb_replacements = array ('<b>\\2</b>','<a href="\\2">\\4</a>','<img scr="\\2">');$string = preg_replace($bb_replace, $bb_replacements, $string);print $string; ?> <br> <br> <br> Tutorial By: <a href = "userinfo.php?user=<?= $_CONTENT['AUTHOR'] ?>"><?= $_CONTENT['AUTHOR'] ?></a> <br> <br></td>[/code]this is the bb code writen in the database,[code][img]http://img220.imageshack.us/img220/6554/button1mv.gif[/img][/code]and it should work like so[img src=\"http://img220.imageshack.us/img220/6554/button1mv.gif\" border=\"0\" alt=\"IPB Image\" /]but its showing up straight text.as for the nl2brI got it working fine! Thanks for that! :) Quote Link to comment https://forums.phpfreaks.com/topic/11100-creating-bb-code/#findComment-41536 Share on other sites More sharing options...
wildteen88 Posted June 3, 2006 Share Posted June 3, 2006 try this:[code]$bb_replace = array ('/(\[[Bb]\])(.+)(\[\/[Bb]\])/', '/(\[url=)(.+)(\])(.+)(\[\/url\])/', '/(\[img\])(.+)(\[\/img\])/');$bb_replacements = array ('<b>\\2</b>', '<a href="\\2">\\4</a>', '<img scr="\\2" />');[/code]basically you forgot to add the ending delimiter which is the backslash '/' Quote Link to comment https://forums.phpfreaks.com/topic/11100-creating-bb-code/#findComment-41546 Share on other sites More sharing options...
me1000 Posted June 3, 2006 Author Share Posted June 3, 2006 Thanks, works like a *charm*Im getting a broken image though![code]<img scr="http://img220.imageshack.us/img220/6554/button1mv.gif" />[/code]That was taken form the source code of the page 9in the browser)The image still works, do you see anything wrong with the code? I dontwould it have anything to do with the dementions not being there? I wouldnt think so, but im a noob! is there a way php can find the demetions of it with out the user having to enter them? Quote Link to comment https://forums.phpfreaks.com/topic/11100-creating-bb-code/#findComment-41548 Share on other sites More sharing options...
wildteen88 Posted June 3, 2006 Share Posted June 3, 2006 Change the following:[code]'<img scr="\\2" />');[/code]to the following:[code]'<img src="\\2" />');[/code]basically you typed [b]scr[/b] instead of [b]src[/b] just a simple type Quote Link to comment https://forums.phpfreaks.com/topic/11100-creating-bb-code/#findComment-41549 Share on other sites More sharing options...
me1000 Posted June 3, 2006 Author Share Posted June 3, 2006 Thanks! all works fine now! Youve been a great help! :)edit: oh one more quick question.is there a way to prevent people form entering HTML or javascript code into the database? Quote Link to comment https://forums.phpfreaks.com/topic/11100-creating-bb-code/#findComment-41552 Share on other sites More sharing options...
wildteen88 Posted June 4, 2006 Share Posted June 4, 2006 yes make sure you use htmlspecialchars or strip_tags on the variable that stores the data that was submitted from the textbox. Quote Link to comment https://forums.phpfreaks.com/topic/11100-creating-bb-code/#findComment-41662 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.