Search code examples
phpmysqlbbcode

php bbcode with mysql retrieve


Below is a sample code from my bbcode array. I will explain after what I need to add.

   $find = array(
    '~\<~s',
    '~\>~s',
    '~\[hr\]~s',
    '~\[b\](.*?)\[/b\]~s',
    '~\[i\](.*?)\[/i\]~s',
    '~\[u\](.*?)\[/u\]~s',
    '~\[s\](.*?)\[/s\]~s',
    '~\[ul\](.*?)\[/ul\]~s',
    '~\[li\](.*?)\[/li\]~s',
    '~\[ol\](.*?)\[/ol\]~s'
);
$replace = array(
    '&lt;',
    '&gt;',
    '<hr>',
    '<b>$1</b>',
    '<i>$1</i>',
    '<span style="text-decoration:underline;">$1</span>',
    '<del>$1</del>',
    '<ul>$1</ul>',
    '<li>$1</li>',
    '<ol>$1</ol>'
);
    $return = preg_replace($find, $replace, $text);
    return nl2br($return);

What I need to do is add an [item] tag that will fetch data from a mysql database.

[item]16[/item] will go into the item table and grab the name & image link using the id 16. Then display: - Name

I've been trying to do this for a while, but am at a dead end. Any advice would be great. Thank you.

In response to @IllegalPigeon.

I was able to modify your code, but I am not getting any results. I was testing it on my main page with a large query. I have cut it down to a test page, running off a variable and still cant get any results. I am using Mysqli and was able to edit the query as needed.

I'm sure I'm on the right track. Might just be missing something stupid.

My current code is:

Working code. Updated from @IllegalPigeon answer.

<?php
//just db stuff.
include("config.php");
// BBcode array
$find = array(
    '~\<~s',
    '~\>~s',
    '~\[hr\]~s',
    '~\[b\](.*?)\[/b\]~s',
    '~\[i\](.*?)\[/i\]~s',
    '~\[u\](.*?)\[/u\]~s',
    '~\[s\](.*?)\[/s\]~s',
    '~\[ul\](.*?)\[/ul\]~s',
    '~\[li\](.*?)\[/li\]~s',
    '~\[ol\](.*?)\[/ol\]~s'
);
$replace = array(
    '&lt;',
    '&gt;',
    '<hr>',
    '<b>$1</b>',
    '<i>$1</i>',
    '<span style="text-decoration:underline;">$1</span>',
    '<del>$1</del>',
    '<ul>$1</ul>',
    '<li>$1</li>',
    '<ol>$1</ol>'
);

$text = "Test text.... [item]6[/item] .... text text";

preg_match_all('#\[item\](.*?)\[/item\]#i', $text, $matches, PREG_SET_ORDER );

for ( $i = 0, $j = count( $matches ); $i < $j; $i++ )
{
    $id = $matches[$i][1];

    if(filter_var($id, FILTER_VALIDATE_INT))
    {
        //It's a number, now you need to do your query
        //You didn't post most so modify your query to look like:
        $sql = "SELECT name, image_url FROM items WHERE id = $id";

        //Assuming you're using PDO, lets check if anything was returned
        if( $result = $db->query($sql) )
        {
        $row = $result->fetch_assoc();
            array_push($find, '~\[item\](.*?)\[/item\]~s');
            array_push($replace, '<img src="' . $row['image_url'] . '" title="' . $row['name'] . '" />');
        } else {
            die('There was an error running the query [' . $db->error . ']');
        }
    }
}
$return = preg_replace($find, $replace, $text);
echo  nl2br($return);

Solution

  • This is completely untested, but, I don't see why it wouldn't work. You're going to have to do some editing on your part, because I don't know what sort of database class you're using, if at all.

    So, try this code:

        $find = array(
            '~\<~s',
            '~\>~s',
            '~\[hr\]~s',
            '~\[b\](.*?)\[/b\]~s',
            '~\[i\](.*?)\[/i\]~s',
            '~\[u\](.*?)\[/u\]~s',
            '~\[s\](.*?)\[/s\]~s',
            '~\[ul\](.*?)\[/ul\]~s',
            '~\[li\](.*?)\[/li\]~s',
            '~\[ol\](.*?)\[/ol\]~s'
        );
        $replace = array(
            '&lt;',
            '&gt;',
            '<hr>',
            '<b>$1</b>',
            '<i>$1</i>',
            '<span style="text-decoration:underline;">$1</span>',
            '<del>$1</del>',
            '<ul>$1</ul>',
            '<li>$1</li>',
            '<ol>$1</ol>'
        );
        preg_match_all('#\[item\](.*?)\[/item\]#i', $text, $matches, PREG_SET_ORDER );
    
        for ( $i = 0, $j = count( $matches ); $i < $j; $i++ )
        {
            $id = $matches[$i][1];
    
            //Lets make sure it is a number.
    
            if(filter_var($id, FILTER_VALIDATE_INT))
            {
                //It's a number, now you need to do your query
                //You didn't post most so modify your query to look like:
                //"SELECT name, image FROM yourTable WHERE id = $id"
    
                //Assuming you're using PDO, lets check if anything was returned
                if( $result = $con->fetch() ) 
                {
                    array_push($find, '~\[item\](.*?)\[/item\]~s');
                    array_push($replace, '<img src="' . $result['image'] . '" title="' . $result['name'] . '" />');
                }
    
            }   
    
        }
        $return = preg_replace($find, $replace, $text);
        return nl2br($return);