Page 1 of 1

blog with BBcodes

Posted: Sun Feb 24, 2013 1:23 pm
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 and other stuff doesnt work, it just outputs without converting..

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

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


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

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

Re: blog with BBcodes

Posted: Sun Feb 24, 2013 2:09 pm
by jacek
I would create a new function for this that converts the BBCode to HTML
function parse_bbcode($input){
   // do things
  return $input;
}
Then use it before the create post/comment function
$message = parse_bbcode($_POST['message']);
something like that anyway.

Re: blog with BBcodes

Posted: Sun Feb 24, 2013 4:28 pm
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!

Re: blog with BBcodes

Posted: Mon Feb 25, 2013 10:27 pm
by gt3000
never mind,SOLVED :D