Jump to content

HomeBrew BBcode


Daleeburg

Recommended Posts

I am trying to make a BBCode deal here and am running into some problems attempting to make a link code.

 

The hope is to make a way so that my user can type in

 

{url=xxxxxxx}yyyyyy{/url}

(imagine brackets where the {} are)

 

and they get out the <a href="xxxxxxxxx">yyyyyyyyyy< /a>

 

or if they put in

 

{url}yyyyyy{/url}

(imagine brackets where the {} are)

 

they get <a href="yyyyyyyy">yyyyyyyyy< /a>

 

I found this snipped of code on the interwebz and did not think it would work, but i tried it anyways

(it didnt work  :o )

(parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')')

 

$BBCode = array(

'/\[url\=(.*?)\](.*?)\[\/url\]/is' => '<a href="$1">$2</a>'

);

$text = str_replace(array_keys($BBCode), array_values($BBCode), $_POST['body']);

 

(there is more to the array then this, but this is the problem line)

 

So if anybody knows how to make this work, or a way to do this and would please share, it would be greatly appreciated

Link to comment
https://forums.phpfreaks.com/topic/39819-homebrew-bbcode/
Share on other sites

tried that, and now it wont post at all, i even tried commenting out and all i get is an did not post message, the variables come out fine though.

 

<?php
FUNCTION addentry(){
IF(ISSET($_SESSION['user'])){


// function BBCode($string, $allowimgs = false) {

        // $from[] = '~\[i\](.*?)\[\/i\]~is';
        // $from[] = '~\[b\](.*?)\[\/b\]~is';
        // $from[] = '~\[u\](.*?)\[\/u\]~is';
        // $from[] = '~\[s\](.*?)\[\/s\]~is';
        // $from[] = '~\[url=(.*?)\](.*?)\[\/url\]~is';
        // $from[] = '~\[url\](.*?)\[\/url\]~is';
        // $from[] = '~\[center\](.*?)\[\/center\]~is';
        // $from[] = '~\[right\](.*?)\[\/right\]~is';
        // $from[] = '~\[left\](.*?)\[\/left\]~is';

        // $to[] = '<i>\\1</i>';
        // $to[] = '<b>\\1</b>';
        // $to[] = '<u>\\1</u>';
        // $to[] = '<del>\\1</del>';
        // $to[] = '<a href="\\1">\\2</a>';
        // $to[] = '<a href="\\1">\\1</a>';
        // $to[] = '<div align="center">\\1</div>';
        // $to[] = '<div align="right">\\1</div>';
        // $to[] = '<div align="left">\\1</div>';

//    we don't want to allow imgs all the time!
        // if ($allowimgs == true) {
           // $from[] = '~\[img\](.*?)\[\/img\]~is';
           // $to[]   = '<img src="\\1" border="0" alt="" />';
        // };

        // $string = preg_replace($from, $to, $string);
         // return $string;
// };

$text = $_POST['body'];

//$text = BBCode($text, true); 

$date = date(DATE_RFC822);
$body = nl2br($text);
$title = $_POST['title'];
$user = $_SESSION['user'];
$query = 'insert into '.$_SESSION['accessdb'].' (title, body, date, who) values (\"'.$title.'\",\"'.$body.'\",\"'.$date.'\",\"'.$_SESSION['user'].'\")';
$result = mysql_query($query) OR DIE("Did Not Post<br>".$_SESSION['accessdb']." <br> $title <br> $body <br> $date <br> $user ");

ECHO 'You have Succefuly Posted! One Moment for the Redirection. <a href=editor.php?mode=2"> Click here </a> If the page does not reload in 15 seconds <meta  http-equiv="refresh" content="2; url=editor.php?mode=2" />';

}ELSE{ECHO "Please Log in";}
};
?>

Link to comment
https://forums.phpfreaks.com/topic/39819-homebrew-bbcode/#findComment-192382
Share on other sites

k i have nailed down my submit problem but i ran into a new one

 

when i try to use the URL commands or the left, center or right for some reason my database will not accept it, any other command works and if i put in the html by hand it works.

 

here is the code

 

<?php
FUNCTION addentry(){
IF(ISSET($_SESSION['user'])){

function BBCode($string, $allowimgs = false) {

        $from[] = '~\[i\](.*?)\[\/i\]~is';
        $from[] = '~\[b\](.*?)\[\/b\]~is';
        $from[] = '~\[u\](.*?)\[\/u\]~is';
        $from[] = '~\[s\](.*?)\[\/s\]~is';
        $from[] = '~\[url=(.*?)\](.*?)\[\/url\]~is';
        $from[] = '~\[url\](.*?)\[\/url\]~is';
        $from[] = '~\[center\](.*?)\[\/center\]~is';
        $from[] = '~\[right\](.*?)\[\/right\]~is';
        $from[] = '~\[left\](.*?)\[\/left\]~is';

        $to[] = '<i>\\1</i>';
        $to[] = '<b>\\1</b>';
        $to[] = '<u>\\1</u>';
        $to[] = '<del>\\1</del>';
        $to[] = '<a href="\\1">\\2</a>';
        $to[] = '<a href="\\1">\\1</a>';
        $to[] = '<div align="center">\\1</div>';
        $to[] = '<div align="right">\\1</div>';
        $to[] = '<div align="left">\\1</div>';

   //we don't want to allow imgs all the time!
        if ($allowimgs == true) {
           $from[] = '~\[img\](.*?)\[\/img\]~is';
           $to[]   = '<img src="\\1" border="0" alt="" />';
        };

        $string = preg_replace($from, $to, $string);
         return $string;
};

$text = $_POST['body'];

$text = BBCode($text, true); 

$date = date(DATE_RFC822);
$body = nl2br($text);
$title = $_POST['title'];

$query = 'insert into '.$_SESSION['accessdb'].' (title, body, date, who) values ("'.$title.'","'.$body.'","'.$date.'","'.$_SESSION['user'].'")';
$result = mysql_query($query) OR DIE("Did Not Post <br> $body ");

ECHO 'You have Succefuly Posted! One Moment for the Redirection. <a href=editor.php?mode=2"> Click here </a> If the page does not reload in 15 seconds <meta  http-equiv="refresh" content="2; url=editor.php?mode=2" />';

}ELSE{ECHO "Please Log in";}
};
?>

 

so the real question is, do i convert it to html before it is put in the database, or do you convert it right before it is viewed?

Link to comment
https://forums.phpfreaks.com/topic/39819-homebrew-bbcode/#findComment-192474
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.