Forum Moderators: coopster

Message Too Old, No Replies

help with my bbcode function

         

bysonary

1:51 pm on Feb 20, 2007 (gmt 0)

10+ Year Member



hello, Someone here very kindly gave me a small example of how to implement a bbcode function, I have expanded this function so it now looks like this.

bbcode.php


<?
function bb2html($text)
{
$trans = array("[b]" => '<b>', "[/b]" => '</b>',
"[u]" => '<u>', "[/u]" => '</u>',
"[i]" => '<i>', "[/i]" => '</i>',
":-)" => '<img src="../images/smile.gif" title="smile :-)">',
";-)" => '<img src="../images/wink.gif" title="wink ;-)">',
"->" => '<img src="../images/arrow.gif" title="arrow">',
">-)" => '<img src="../images/badfrin.gif" title="bad grin">',
":-D" => '<img src="../images/biggrin.gif" title="big grin">',
":-S" => '<img src="../images/confused.gif" title="confused">',
"0-)" => '<img src="../images/cool.gif" title="cool">',
";-(" => '<img src="../images/cry.gif" title="cry">',
"/-¦" => '<img src="../images/doubt.gif" title="doubt">',
":-@" => '<img src="../images/evil.gif" title="evil">',
"<!>" => '<img src="../images/exclaim.gif" title="exlamation">',
"<i>" => '<img src="../images/idea.gif" title="idea">',
"<lol>" => '<img src="../images/arrow.gif" title="lol">',
">-<" => '<img src="../images/mad.gif" title="mad">',
":-¦" => '<img src="../images/neutral.gif" title="nuetral">',
"<?>" => '<img src="../images/question.gif" title="question">',
":-P" => '<img src="../images/raz.gif" title="raz">',
"$-¦" => '<img src="../images/redface.gif" title="red face">',
"8-¦" => '<img src="../images/rolleys.gif" title="rolly eyes">',
":-(" => '<img src="../images/sad.gif" title="sad">',
">-O" => '<img src="../images/shock.gif" title="shock">',
":-O" => '<img src="../images/suprised.gif" title="suprised">',
"[quote]" => '<table bgcolor='#cccccc'><tr><td style='border: solid 1px #000000'>', "[/quote]" => '</td></tr></table>');

$translated = strtr($text, $trans);
return $translated;
}
?>

I do however have a small problem this works fine first time when I add the data to the database, I am using the following code to add the data to the database

newsadd.php


<?php
include '/home/www/juttuffi/auth.php';
include '/home/www/juttuffi/dbc.php';
include '/home/www/juttuffi/news/admin/bbcode.php';

$pattern = "/(http:\/\/[\w\.]+)/";
$replace = "<a href='$1'>$1</a>";

$title = $_POST['title'];
$news = $_POST['news'];

if (!empty($title) &&!empty($news))
{
$title = preg_replace($pattern, $replace, $title);
$news = preg_replace($pattern, $replace, $news);

$title = bb2html($title);
$news = bb2html($news);

$title = mysql_real_escape_string($title);
$news = mysql_real_escape_string(nl2br($news));
$date = date("D d M Y, g:i a");

$sqlquery = "INSERT INTO tnews (title, body, date) VALUES ('$title','$news','$date')";

header("Location: index.php");

//print $sqlquery;
$results = mysql_query($sqlquery);
}
else
{
include '/home/www/juttuffi/header.php';
echo '<san class="notice">';
echo '***ERROR: Please make sure you have entered a title and some news***';
echo '</span>';
echo '<br>';
include '/home/www/juttuffi/news/admin/newsaddform.php';
}

mysql_close($dbc);
?>

This works fine the smilies appear fine in the output, however when i use my edit script which is below i get some distorted output, here is my edit script.

newsedit.php


<?php
include '/home/www/juttuffi/auth.php';
include '/home/www/juttuffi/dbc.php';
include '/home/www/juttuffi/news/admin/bbcode.php';

$pattern = "/(http:\/\/[\w\.]+)/";
$replace = "<a href='$1'>$1</a>";

$title = $_POST['title'];
$news = $_POST['news'];
$id = (int)$_POST["id"];

if (!empty($title) &&!empty($news))
{
$title = preg_replace($pattern, $replace, $title);
$news = preg_replace($pattern, $replace, $news);

$title = bb2html($title);
$news = bb2html($news);

$title = mysql_real_escape_string($title);
$news = mysql_real_escape_string(nl2br($news));

$sqlquery = "UPDATE tnews SET title='$title', body='$news' WHERE id='$id'";

header("Location: index.php");

//print $sqlquery;
$results = mysql_query($sqlquery);
}
else
{
include '/home/www/juttuffi/header.php';
echo '<span class="notice">';
echo '***ERROR: Please make sure that yu have not tried to submit any empty fields***';
echo '</span>';
echo '<br>';
include '/home/www/juttuffi/news/admin/newseditform.php';
}

mysql_close($dbc);

?>

as you can see the edit code is almost identical to the add code, however say i added a simple news post which contained one smiley.

:-), this would appear as a smiley fine but then say i edited the post and didn't change anything but just clicked "edit" you would expect there to be no change in the output and for it to remain a smiley

:-)

instead i get something like this after editing

:-)"\>

can anyone help me locate the problem here and perhaps tell me how to solve it, I would be extremely grateful.

dreamcatcher

2:08 pm on Feb 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi bysonary,

Your problem is most probably caused by HTML tags in your text area. Try formatting the BBCode when its displayed only and not when you add your data to the database. htmlspecialchars [php.net] is usually a help with textareas, but if you use this you might prevent your HTML from display properly.

dc

bysonary

3:22 pm on Feb 20, 2007 (gmt 0)

10+ Year Member



This is the code for the textarea in the newseditform.php file:

<textarea name="news" cols="40" rows="15" id="news"><?php echo $row['body'];?></textarea>

Can anyone explain how this is causing the problems?

bysonary

3:39 pm on Feb 20, 2007 (gmt 0)

10+ Year Member



fixed it mate, i did what you said and made it so the function is used as the data is displayed rather than when it is added to the database.

Thanks for your help, i think i found out the problem,

it was performing bb2html when it was being added and then when it was being edited it was trying to perform the function again, this was resulting in two instances of the smiley wrapped up in eachother kind of like this

<img src="../images/smile.gif" title="smile <img src="../images/smile.gif" title="smile :-)">">

bysonary

3:46 pm on Feb 20, 2007 (gmt 0)

10+ Year Member


whilst on the same topic of the bbcode function can anyone tell me the code to sort of convert something wrapped in (url) (/url) bbcode tags? e.g.

(url=http://google.co.uk)LINK(/url)

would link to google.co.uk with the text LINK

its the only thing i am having bother with, kind of.

dreamcatcher

4:14 pm on Feb 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


Glad your getting on the right track. For urls, something like this should do:

$data = 'This is a [url=http://www.google]Google[/url]';

$data = eregi_replace("\\[url=([^\\[]*)\\]([^\\[]*)\\[/url\\]", "<a href=\"\\1\">\\2</a>", $data);

echo $data;

dc

bysonary

4:26 pm on Feb 20, 2007 (gmt 0)

10+ Year Member



is there nothing along the lines of

"[url]" => '<a href="$url" target="_blank">', "[/url]" => '</a>';