Jump to content

Recommended Posts

function bbcode($var) {
$search = array(
	'/\[s\](.*?)\[\/s\]/is',        
	'/\[b\](.*?)\[\/b\]/is', 
	'/\[i\](.*?)\[\/i\]/is',                                
	'/\[u\](.*?)\[\/u\]/is',
	'/\[img\](.*?)\[\/img\]/is',
	'/\[url\](.*?)\[\/url\]/is',
	'/\[url\=(.*?)\](.*?)\[\/url\]/is',
	'/\[php\](.*?)\[\/php\]/is'
	);
$replace = array(
	'<s>$1</s>',
	'<strong>$1</strong>',
	'<em>$1</em>',
	'<u>$1</u>',
	'<img src="$1" />',
	'<a href="$1">$1</a>',
	'<a href="$1">$2</a>',
	''
	);
if(stristr($var, '[code=php:0]') == true){
	$search2 = array(
		'/\[php\](.*?)\[\/php\]/is'
		);
	$replace2 = array(
		'$1'
		);
	$var = preg_replace ($search2, $replace2, $var);
	//$var = preg_replace ("/javascript:/i","http://javascript*******",$var);
	return $var;
}
	$var = preg_replace ($search, $replace, $var);
	$var = preg_replace ("/javascript:/i","http://javascript*******",$var);
	return $var;
}

 

Well i'm trying to help oujt my friend but I know the answer but I don't have time to do it, I understand that the way he wrote his function is beyond me and would require me to rewrite it, But I don't feel like rewriting it, So could you guys help me out and find a solution?

Link to comment
https://forums.phpfreaks.com/topic/71697-php-bbcode-php-function/
Share on other sites

sorry let me POST the original source and ill explain, I'm sorry I forgot xD.

 

$search = array(
	'/\[s\](.*?)\[\/s\]/is',        
	'/\[b\](.*?)\[\/b\]/is', 
	'/\[i\](.*?)\[\/i\]/is',                                
	'/\[u\](.*?)\[\/u\]/is',
	'/\[img\](.*?)\[\/img\]/is',
	'/\[url\](.*?)\[\/url\]/is',
	'/\[url\=(.*?)\](.*?)\[\/url\]/is',
	'/\[php\](.*?)\[\/php\]/is'
	);

$replace = array(
	'<s>$1</s>',
	'<strong>$1</strong>',
	'<em>$1</em>',
	'<u>$1</u>',
	'<img src="$1" />',
	'<a href="$1">$1</a>',
	'<a href="$1">$2</a>',
	'<center><table style="border-style: dashed; background-color: #FFF; border-width: thin;" align="left"><tr><td>
	<?php highlight_string(\' ?>$1<?php \'); ?></tr></td></table></center>'
	);

 

Well He's tryting to get the highlight_string(); to work inside of an array and its not working, At all. and well I know it wouldn't I took and checked other popular forums that use

 tag. and checked them out, They make a function specially for the [code=php:0] BBcode. 

You'll want to use the e pattern modifier if you want to call a function in the replacement. However you cannot have html and php code in the replacement together if you use this modifier. That is why other third party apps use functions for parsing php code.

 

Working example:

<?php

$search = array( '/\[s\](.*?)\[\/s\]/is',
                 '/\[b\](.*?)\[\/b\]/is',
                 '/\[i\](.*?)\[\/i\]/is',
                 '/\[u\](.*?)\[\/u\]/is',
                 '/\[img\](.*?)\[\/img\]/is',
                 '/\[url\](.*?)\[\/url\]/is',
                 '/\[url\=(.*?)\](.*?)\[\/url\]/is',
                 '/\[php\](.*?)\[\/php\]/ies'
);

$replace = array( '<s>$1</s>',
                  '<strong>$1</strong>',
                  '<em>$1</em>',
                  '<u>$1</u>',
                  '<img src="$1" />',
                  '<a href="$1">$1</a>',
                  '<a href="$1">$2</a>',
                  'highlightPHP(\'$1\')'
);

function highlightPHP($code)
{
    $html = '<table style="border-style: dashed; background-color: #FFF; border-width: thin;" align="center">
<tr><td align="left">' . highlight_string($code, true) . '</tr></td></table>';

    return $html;
}


$text = 'Hello [i]world[/i]

[code=php:0]<?php

echo \'hello\';

?>

 

Done';

 

$html = preg_replace($search, $replace, $text);

 

echo nl2br($html);

 

?>[/code]

Your example doesn't work me when I try to intergrate.

 

function bbcode($var) {
$search = array( '/\[s\](.*?)\[\/s\]/is',
	'/\[b\](.*?)\[\/b\]/is',
	'/\[i\](.*?)\[\/i\]/is',
	'/\[u\](.*?)\[\/u\]/is',
	'/\[img\](.*?)\[\/img\]/is',
	'/\[url\](.*?)\[\/url\]/is',
	'/\[url\=(.*?)\](.*?)\[\/url\]/is',
	'/\[php\](.*?)\[\/php\]/ies'
);

$replace = array( '<s>$1</s>',
	'<strong>$1</strong>',
	'<em>$1</em>',
	'<u>$1</u>',
	'<img src="$1" />',
	'<a href="$1">$1</a>',
	'<a href="$1">$2</a>',
	'highlightPHP(\'$1\')'
);

	//$var = preg_replace($search, $replace, $var);
	//$var = preg_replace ($search, $replace, $var);
	//$var = preg_replace ("/javascript:/i","http://javascript*******",$var);
	$htmlx = preg_replace($search, $replace, $var);
	//echo nl2br($htmlx);
	return $htmlx;
}

function highlightPHP($code)
{
    $html = '<table style="border-style: dashed; background-color: #FFF; border-width: thin;" align="center">
<tr><td align="left">' . highlight_string($code, true) . '</tr></td></table>';

    return $html;
}

 

D: tell me what I did wrong here. it just add's < /BR> tag's.

[code=php:0]
<?php
echo "Hello world!";
?>

[/code]

EDIT: somewhere in this file,

 

<?php
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//  ____  ____  ____  ____  ____ 
// (_  _)(  _ \(_  _)(  _ \(  _ \
//  _)(_  ) __/  )(   ) _ < ) _ <
// (____)(__)   (__) (____/(____/
//			LITE Version 0.5.0
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// IPTBB is free software.
// http://iptbb.org/LICENSE.txt
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//Include the mysql files, and functions
include('config.php');
include('functions.php');
$tid=$_GET['id'];

//Get the settings
$setting = array();
$sql = mysql_query(" SELECT * FROM `iptbb_settings` ");

while ( $row = mysql_fetch_array( $sql ) ){
$setting["{$row['name']}"] = $row['value'];
}

//Include the template maker
require_once('tpl.class.php');
$tpl = new template;
$fileurl = 'templates/';
$template = $setting['template'] . '/';

//Set some links
$links = '<a href="index.php">Index</a> · <a href="memberlist.php">Member List</a>';

//Check if they are logged in or not.
if( isset($_COOKIE['username']) AND isset($_COOKIE['password']) ){
//Select the user from the database
$user=$_COOKIE['username'];
$pass=$_COOKIE['password'];
$login_query = mysql_query( "SELECT * FROM `iptbb_users` WHERE `username` = '$user' AND `password` = '$pass' " );
$login_rows = mysql_num_rows($login_query);

if($login_rows > 0){
	//If they are logged in
	$login_row = mysql_fetch_array( $login_query );

	$now = time();
	$insertuser = useronline($user, $now);

	if( $login_row['level'] == '2' ){
		//If they are an admin
		$links .= ' · <a href="admincp.php">Admin CP</a>';
	}	
	$links .= ' · <a href="usercp.php?act=home">User CP</a> · <a href="logout.php">Logout</a>';
} else {
	//If they are not in our user list
	$links .= ' · <a href="register.php">Register</a> ';
}
} else {
//If they have no cookies set
$links .= ' · <a href="register.php">Register</a> ';
}

//Get the topic deatils
$topic_query = mysql_query( "SELECT * FROM `iptbb_topics` WHERE `tid` = '$tid' " );
$topic_row = mysql_fetch_array( $topic_query );

//Create the webpage
$tpl->assign(array(
    'title' => $setting['title'] . ' - Viewing Topic: ' . $topic_row['title'],
'template' => $setting['template'],
'description' => $setting['description'],
'keywords' => $setting['keywords'],
'author' => $setting['author'],
));
$tpl->display(''.$fileurl.$template.'header.tpl');

//Display the login box
if( ($login_rows <= 0) OR ( !isset($_COOKIE['username']) OR !isset($_COOKIE['password']) ) ){
$tpl->display(''.$fileurl.$template.'login_form.tpl');
}

//Display the menu
$tpl->assign(array(
'menu' => $links,
));
$tpl->display(''.$fileurl.$template.'menu.tpl');

//Display the announcement box
if( ($setting['news_show'] == '1') AND ($setting['global_news'] == '1') ){
$tpl->assign(array(
	'announcement' => $setting['news'],
));
$tpl->display(''.$fileurl.$template.'announcement.tpl');
}

//Display forum
$fid = $topic_row['fid'];
$starter = $topic_row['starter'];

//Set the views
$views = $topic_row['views'];
++$views;
mysql_query("UPDATE `iptbb_topics` SET `views` = '$views' WHERE `tid` = '$tid' ");

//Get the forum details
$forum_query = mysql_query( "SELECT * FROM `iptbb_forums` WHERE `fid` = '$fid' " );
$forum_row = mysql_fetch_array( $forum_query );
$cid = $forum_row['cid'];

//Get the category details
$category_query = mysql_query( "SELECT * FROM `iptbb_categories` WHERE `cid` = '$cid' " );
$category_row = mysql_fetch_array( $category_query );

//Get the user details
$user_query = mysql_query( "SELECT * FROM `iptbb_users` WHERE `id` = '$starter' " );
$user_num = mysql_num_rows( $user_query );
$user_row = mysql_fetch_array( $user_query );

if( $user_num > 0 ){

//If the user exists
if( $user_row['avatar'] == '' ){
	//If the avatar does not exist
	$avatar = ''.$fileurl.$template.'images/default_avatar.png';
} else {
	//If the avatar exists
	$avatar = $user_row['avatar'];
}
$posts = $user_row['posts'];
$name = '<a href="profile.php?id=' . $user_row['id'] . '">' . $user_row['username'] . '</a>';
$user = $user_row['username'];
if( $user_row['sig'] == '' ){
	//If there is no signature
	$signature = '';
} else {
	//If there is a signature
	$signature = '<hr size="1" width="25%"  align="left" />' . nl2br($user_row['sig']);
}

//Get the level
if( ($user_row['level'] == 1) OR ($user_row['level'] == 0)  ){
	$level = 'Member';
} elseif( $user_row['level'] == 2 ){
	$level = 'Administrator';
}

} else {

//If the user does not exist
$avatar = ''.$fileurl.$template.'images/default_avatar.png';
$posts = 'N/A';
$name = $starter;
$signature = ' ';
$user = $topic_row['starter'];

}

//Check what level the viewing user is
if( $login_row['level'] >= 0 ){

$reply = '<a href="createreply.php?tid='.$topic_row['tid'].'&pid=0">Reply</a>';

if( ($login_row['id'] == $user_row['id']) OR ($login_row['level'] >= 2) ){

	$edit = '<a href="edit.php?tid='.$topic_row['tid'].'&pid=0">Edit Topic</a> ·';

	if( $login_row['level'] >= 2 ){

		$delete = '<a href="delete.php?tid='.$topic_row['tid'].'&pid=0">Delete Topic</a> ·';

	} else {
		$delete = '';
	}	
} else {
	$edit = '';
	$delete = '';	
}
} else {
$reply = '';
$edit = '';
$delete = '';
}

if( $topic_row['edituser'] == '0' ){
$mod = '';
} else {
$user_query = mysql_query( "SELECT * FROM `iptbb_users` WHERE `id` = '$topic_row[edituser]' " );
$user_row = mysql_fetch_array( $user_query );
$mod = 'Last edited on ' . time_date( $topic_row['editdate'] ) . ' by ' . $user_row['username'] . '.';
}

//Display the start of the topic
$tpl->assign(array(
'bName' => $setting['name'],
'dLink' => 'viewdir.php?id=' . $cid,
'dName' => $category_row['name'],
'fLink' => 'viewforum.php?id=' . $fid,
'fName' => $forum_row['name'],

'tName' => stripslashes($topic_row['title']),
'tContent' => bbcode(nl2br(stripslashes($topic_row['body']))),
'username' => $name,
'image' => $avatar,
'posts' => $posts,
'signature' => bbcode(nl2br(stripslashes($signature))),
'level' => $level,
'date' => time_date( $topic_row['date'] ),
'reply' => $reply,
'edit' => $edit,
'delete' => $delete,
'mod' => $mod
));
$tpl->display(''.$fileurl.$template.'topic_head.tpl');

$post_query = mysql_query( "SELECT * FROM `iptbb_posts` WHERE tid='$tid' ORDER BY `pid` ASC " );
while( $post_row = mysql_fetch_array( $post_query ) ){

//Get the user details
$user_query = mysql_query( "SELECT * FROM `iptbb_users` WHERE `id` = '$post_row[user]' " );
$user_num = mysql_num_rows( $user_query );
$user_row = mysql_fetch_array( $user_query );

if( $user_num > 0 ){

	//If the user exists
	if( $user_row['avatar'] == '' ){
		//If the avatar does not exist
		$avatar = ''.$fileurl.$template.'images/default_avatar.png';
	} else {
		//If the avatar exists
		$avatar = $user_row['avatar'];
	}
	$posts = $user_row['posts'];
	$name = '<a href="profile.php?id=' . $user_row['id'] . '">' . $user_row['username'] . '</a>';

	if( $user_row['sig'] == '' ){
		//If there is no signature
		$signature = '';
	} else {
		//If there is a signature
		$signature = '<hr size="1" width="25%"  align="left" />' . nl2br($user_row['sig']);
	}

	//Get the level
	if( $user_row['level'] == 1 OR $user_row['level'] == 0) {
		$level = 'Member';
	} elseif( $user_row['level'] == 2 ){
		$level = 'Administrator';
	}

	//Check what level the viewing user is
	if( $login_row['level'] >= 0 ){

		$reply = '<a href="createreply.php?tid='.$topic_row['tid'].'&pid='.$post_row['pid'].'">Reply</a>';

		if( ($login_row['id'] == $user_row['id']) OR ($login_row['level'] >= 2) ){

			$edit = '<a href="edit.php?tid='.$topic_row['tid'].'&pid='.$post_row['pid'].'">Edit</a> ·';

			if( $login_row['level'] >= 2 ){

				$delete = '<a href="delete.php?tid='.$topic_row['tid'].'&pid='.$post_row['pid'].'">Delete</a> ·';

			} else {
				$delete = '';
			}	
		} else {
			$edit = '';
			$delete = '';	
		}
	} else {
		$reply = '';
		$edit = '';
		$delete = '';
	}

	if( $post_row['edituser'] == '0' ){
		$mod = '';
	} else {
		$user_query = mysql_query( "SELECT * FROM `iptbb_users` WHERE `id` = '$post_row[edituser]' " );
		$user_row = mysql_fetch_array( $user_query );
		$mod = 'Last edited on ' . time_date( $post_row['editdate'] ) . ' by ' . $user_row['username'] . '.';
	}

	//Display the normal template
	$tpl->assign(array(
		'tName' => stripslashes($post_row['title']),
		'tContent' => bbcode(nl2br(stripslashes($post_row['body']))),
		'username' => $name,
		'image' => $avatar,
		'posts' => $posts,
		'signature' => bbcode(nl2br(stripslashes($signature))),
		'level' => $level,
		'date' => time_date( $post_row['date'] ),
		'reply' => $reply,
		'edit' => $edit,
		'delete' => $delete,
		'mod' => $mod,
	));
	$tpl->display(''.$fileurl.$template.'topic_mid.tpl');

} else {

	//If the user does not exist
	$name = $post_row['user'];

	//Display the normal template
	$tpl->assign(array(
		'tName' => stripslashes($post_row['title']),
		'tContent' => bbcode(nl2br(stripslashes($topic_row['body']))),
		'username' => $name,
		'date' => time_date( $post_row['date'] ),
		'tid' => $topic_row['tid'],
		'id' => $post_row['pid'],
	));
	$tpl->display(''.$fileurl.$template.'topic_mid2.tpl');

}
}
//Display the finishing of the table
$tpl->display(''.$fileurl.$template.'bot_table.tpl');

//Displaying Statistics
if( ($setting['statistics'] == 1) AND ($setting['viewtopic_stats'] == 1) ){
include_once( 'statistics.php' );
}

//Display the footer
$tpl->assign(array(
	'copyright' => $setting['copyright'] . '<br /> Powered by <a href="http://iptbb.org/">IPTBB</a> :: Version ' . $setting['version'],
));
$tpl->display(''.$fileurl.$template.'footer.tpl');

?>

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.