Jump to content


Photo

creating bb code


  • Please log in to reply
7 replies to this topic

#1 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 03 June 2006 - 05:52 PM

ok I want to create bb codes,
I found this little script. but it doesnt support images.

          $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;

Ive already installed it ant it works fine, (except the image part)

$bb_replace = array ('/(\[[Bb]\])(.+)(\[\/[Bb]\])/','/(\[url=)(.+)(\])(.+)(\[\/url\])/','/(\[img])(.+)(\[\/img\]);
$bb_replacements = array ('<b>\\2</b>','<a href="\\2">\\4</a>','<img scr="\\2">');

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?

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 June 2006 - 06:51 PM

Change your code to this:
$bb_replace = array ('/(\[[Bb]\])(.+)(\[\/[Bb]\])/','/(\[url=)(.+)(\])(.+)(\[\/url\])/','/(\[img\])(.+)(\[\/img\]);
$bb_replacements = array ('<b>\\2</b>','<a href="\\2">\\4</a>','<img scr="\\2">');

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.

#3 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 03 June 2006 - 07:31 PM

Thanks for the help, but it is still not working! :S

this is my code that displays the info

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

this is the bb code writen in the database,
[img]http://img220.imageshack.us/img220/6554/button1mv.gif[/img]
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 nl2br

I got it working fine!

Thanks for that! :)

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 June 2006 - 07:49 PM

try this:
$bb_replace = array ('/(\[[Bb]\])(.+)(\[\/[Bb]\])/',
                     '/(\[url=)(.+)(\])(.+)(\[\/url\])/',
                     '/(\[img\])(.+)(\[\/img\])/');

$bb_replacements = array ('<b>\\2</b>',
                          '<a href="\\2">\\4</a>',
                          '<img scr="\\2" />');

basically you forgot to add the ending delimiter which is the backslash '/'

#5 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 03 June 2006 - 08:00 PM

Thanks, works like a *charm*
Im getting a broken image though!

<img scr="http://img220.imageshack.us/img220/6554/button1mv.gif" />

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 dont

would 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?

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 June 2006 - 08:04 PM

Change the following:
'<img scr="\\2" />');
to the following:
'<img src="\\2" />');
basically you typed scr instead of src just a simple type

#7 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 03 June 2006 - 08:11 PM

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?

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 June 2006 - 10:21 AM

yes make sure you use htmlspecialchars or strip_tags on the variable that stores the data that was submitted from the textbox.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users