blog with BBcodes

Post here is you are having problems with any of the tutorials.
Post Reply
gt3000
Posts: 11
Joined: Wed Feb 20, 2013 11:46 am

blog with BBcodes

Post by gt3000 »

Hi!I have complited this nice blog tutorial,everything works fine,thanks to Author of this tutorial!
And now I have a question, is it possible to make a BBcodes in Blog?When user write (some text) it will show text in bold?

I tried to add this parser or how it's called for example into my blog_read.php but [b] and other stuff doesnt work, it just outputs without converting..

so here is that parser or what...and it works

[syntax=php] <html>

<head>
<title>BBCode Parser</title>
</head>



<body>

<?php
if(isset($_POST['message']))
{
$text = $_POST['message'];
$text = stripslashes($text);
$text = htmlspecialchars($text);
$text = nl2br($text);

$text = preg_replace('#\[b\](.+)\[/b\]#isU', '<b>$1</b>', $text);
$text = preg_replace('#\[i\](.+)\[/i\]#isU', '<i>$1</i>', $text);
$text = preg_replace('#\[u\](.+)\[/u\]#isU', '<u>$1</u>', $text);

$text = preg_replace('#\[img\](.+)\[/img\]#isU', '<img src="$1" />', $text);

echo $text;
}
else
{
?>

<form action='bbcodeparser.php' method='POST'>

<textarea name='message'>TEXT GOES HERE</textarea></br>
<input type='submit' value='Submit' />

</form>

<?php
}
?>

</body>

</html>
[/syntax]

So if I try like this,I made a new separate page ,than it works.So the question is,where I should put this code into my blog_read and other blog files?I hope for your help"BetterPhp" :ugeek:

here is my experiment with blog_read.php and with parser inside

[syntax=php]<?php


include('core/init.inc.php');

if (isset($_GET['pid'], $_POST['user'], $_POST['body'])){
if (add_comment($_GET['pid'], $_POST['user'], $_POST['body'])){
header("Location: blog_read.php?pid={$_GET['pid']}");
}else{
header('Location: blog_list.php');
}


}
?>
<!DOCTYPE HTML>

<html lang="en" class="no-js">
<head>
<meta charset="UTF-8">

<title>SITENAME</title>

</head>
<body>
<div>
<?php

if (isset($_GET['pid']) === false || valid_pid($_GET['pid']) === false){
echo 'Invalid post ID.';
}else{
$post = get_post($_GET['pid']);

?>
<h2><?php echo $post['title']; ?></h2>
<h4>By <?php echo $post['user']; ?> on <?php echo $post['date']; ?> (<?php echo count($post['comments']); ?> comments)</h4>

<hr />

<p><?php echo $post ['body']; ?></p>
<hr />
<?php

foreach ($post['comments'] as $comment){
?>
<h4>By <?php echo $comment['user']; ?> on <?php echo $comment['date']; ?></h4>
<p><?php echo $comment['body']; ?></p>
<hr />
<?php
}

?>

<?php
if(isset($_POST['body']))
{
$text = $_POST['body'];
$text = stripslashes($text);
$text = htmlspecialchars($text);
$text = nl2br($text);

$text = preg_replace('#\[b\](.+)\[/b\]#isU', '<b>$1</b>', $text);
$text = preg_replace('#\[i\](.+)\[/i\]#isU', '<i>$1</i>', $text);
$text = preg_replace('#\[u\](.+)\[/u\]#isU', '<u>$1</u>', $text);

$text = preg_replace('#\[img\](.+)\[/img\]#isU', '<img src="$1" />', $text);

echo $text;
}
else
{
?>




<form action='' method="POST">
<p>
<label for="user">Name</label>
<input type="text" name="user" id="user" />
</p>
<p>
<textarea name="body">text here</textarea>
</p>
<p>
<input type="submit" value="Add Comment" />
</p>
</form>
<?php
}
}

?>
</div>
</body>
</html>[/syntax]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: blog with BBcodes

Post by jacek »

I would create a new function for this that converts the BBCode to HTML

[syntax=php]function parse_bbcode($input){
// do things
return $input;
}[/syntax]
Then use it before the create post/comment function

[syntax=php]$message = parse_bbcode($_POST['message']);[/syntax]
something like that anyway.
Image
gt3000
Posts: 11
Joined: Wed Feb 20, 2013 11:46 am

Re: blog with BBcodes

Post by gt3000 »

thanks for your reply!
I tried that with functions and other ways,but nothing.
So if I type something using BBcode,it shows on the screen,but when I refresh page I cant see it anymore.
i realy want to make this thing,I need to make,that I can copy paste links from imagechack(image hosting website) or how they are called, and when I type this link I get a little preview or something like that in blog. So the main thing I need to get images previews from imagechack forexample.

i realy like your tutorials and how you explain,the best tutorials only you have.A Blog tutorial was realy amaZing,everything works perfect,I am just starting to learn PhP,was sitting all this weekend in search of solution for the BBcode.
If it's not hard, can you please explain more detailed where should I put function with BBcode..
It's very important,maybe it will help to others also :)

Thanks!
gt3000
Posts: 11
Joined: Wed Feb 20, 2013 11:46 am

Re: blog with BBcodes

Post by gt3000 »

never mind,SOLVED :D
Post Reply