Jump to content

BBCode help


bloodgoat

Recommended Posts

Working on the BBCode section of my shoutbox script. A few errors I can't seem to wrap my head around. Here are the few I have so far:

<?php
for($i=0;$i<count($post);$i++){
	$post[$i][3] = str_replace(":as:", "<img src=\"http://i204.photobucket.com/albums/bb303/img0t/as.png\">", $post[$i][3]);
	$post[$i][3] = str_replace("[b]", "<b>", $post[$i][3]);
	$post[$i][3] = str_replace("[/b]", "</b>", $post[$i][3]);
	$post[$i][3] = str_replace("[i]", "<i>", $post[$i][3]);
	$post[$i][3] = str_replace("[/i]", "</i>", $post[$i][3]);
}
?>

Everything works fine. My trouble is, I'm trying to implement a BBCode URL tag. A bit like so:

<?php
	$post[$i][3] = str_replace("[url=", "<a target=\"_blank\" href=\"", $post[$i][3]);
	$post[$i][3] = str_replace("]", "\">", $post[$i][3]);
	$post[$i][3] = str_replace("[/url], "</a>", $post[$i][3]);
?>

The problem being, since "]" is set as it's own tag with it's own specific string replacement, it destroys the [­b][­/b] and [­i][­/i] tags. On output after putting URL tags in, where bold text WOULD go, it instead looks like: [b">I'm attempting to bold this statement[/b">

 

So, how can I circumvent this? I thought briefly that changing the order of the string replacements might alleviate this, but to no avail. So... any words of wisdom?

Link to comment
Share on other sites

Bah, I wish my brain knew how to parse preg_replace strings. It all just looks like nonsense to me.

 

Regardless...

How about this:

 

<?php
    $post[$i][3] = preg_replace('#\[url=(.*?)\](.*?)\[/url\]#i', '<a href="$1">$2</a>', htmlentities($post[$i][3]));
?>

 

Cheers

Chris

This worked just fine, until I noticed it broke my test smiley. The output for that afterwards remains to be the normal <­img src="url"> tag.

Link to comment
Share on other sites

Probably because we used htmlentities().

 

Try using this:

 

<?php
    function BBCode($post) {
        $post = htmlentities($post);
        
        $search = array(
                        '#\[b\](.*?)\[/b\]#is',
                        '#\[i\](.*?)\[/i\]#is',
                        '#\[url=(.*?)\](.*?)\[/url\]#i',
                        '#\:as\:#i'
                    );
                    
        $replace = array(
                        '<strong>$1</strong>',
                        '<em>$1</em>',
                        '<a href="$1" title="$2">$2</a>',
                        '<img alt="" src="http://i204.photobucket.com/albums/bb303/img0t/as.png" />'
                    );
                    
        $post = preg_replace($search, $replace, $post);
        
        $post = nl2br($post);
        
        return $post;
    }
    
    for($i = 0; $i < count($post); $i++) {
        $post[$i][3] = BBCode($post[$i][3]);
    }
?>

 

Cheers

Chris

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.