converting html code to BBcode

Discussion forum for MOD Writers regarding MOD Development.
User avatar
joeroberts
Registered User
Posts: 272
Joined: Fri Jun 22, 2007 7:19 pm

converting html code to BBcode

Post by joeroberts »

I am working on making a auto insert of a new topic and I created a new function to convert HTML code to BBcode and need some input on this as it wont allow the new code to desplay

Code: Select all

function html_bbcode_format ($replace) { 
$replace = str_replace('<br />','',$replace);
#$replace = preg_replace("<img vspace=\"([1-7])\" align=\"((\s|.)+?)\" alt=\"\" src=\"(http:\/\/[^\s'\"<>]+(\.gif|\.jpg|\.png))\">", "[img=$4]", $replace);
$replace = preg_replace("<img alt=\"\" src=\"(http:\/\/[^\s'\"<>]+(\.gif|\.jpg|\.png))\">", "[img]$1[/img]", $replace);
$replace = preg_replace("<img src=\"(http:\/\/[^\s'\"<>]+(\.gif|\.jpg|\.png))\" alt=\"\" >", "[img]$1[/img]", $replace);

$replace = preg_replace('/\<font size=\"([1-7])\"\>((\s|.)+?)\<\/font>/i', '[size=\\1]\\2[/size]', $replace);
$replace = preg_replace('/\<font color=\"(#[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9])\"\>((\s|.)+?)\<\/font>/i', '[color=\\1]\\2[/color]', $replace);
$replace = preg_replace('/\<font color=\"([a-zA-Z]+)\]((\s|.)+?)\<\/font>/i', '[color=\\1]\\2[/color]', $replace);
$replace = preg_replace("/\<a href=\"((http|ftp|https|ftps|irc):\/\/[^<>\s]+?)\">((\s|.)+?)\<\/a\>/i","[url=\\1]\\3[/url]", $replace);
$replace = str_replace('<i>','[i]',$replace);
$replace = str_replace('</i>','[/i]',$replace);
$replace = str_replace('<b>','[b]',$replace);
$replace = str_replace('</b>','[/b]',$replace);
$replace = str_replace('<u>','[u]',$replace);
$replace = str_replace('</u>','[/u]',$replace);
$replace = str_replace('<','',$replace);
$replace = str_replace('/>','',$replace);
$replace = str_replace('&nbsp;',' ',$replace);
return $replace; 
}
  $replace ='testing topic <a href="http://something.org/phpBB.php?page=viewforum&f=2">insert </a>Description<br />
<br />
<br />
<br />
<br />
&nbsp;<img src="http://www.something.net/UserFiles/Image/29bruce_psoter.jpg" alt="" /><br />
<font color="#99cc00"><font size="4">t</font><font size="4"><br />
</font> </font>'; 
echo  html_bbcode_format ($replace);
      

this returns

Code: Select all

testing topic [url=http://something.org/phpBB.php?page=viewforum&f=2]insert[/url]Description [img]http://www.something.net/UserFiles/Image/29bruce_psoter.jpg[/img] [color=#99cc00][size=4]t[/size] [/color]
I see in the topic table that it is looking for
bbcode_bitfield
and
bbcode_uid
but Im not sure how they are created.
I copied one from another insert but it did not work it just came back blank
any one have a clue what I may have missed?
I can copy my new code to a topic and it works %100
User avatar
A_Jelly_Doughnut
Former Team Member
Posts: 34459
Joined: Sat Jan 18, 2003 1:26 am
Location: Where the Rivers Run

Re: converting html code to BBcode

Post by A_Jelly_Doughnut »

You can use the function generate_text_for_storage() (/includes/functions.php) to create the UID and bitfield if memory serves.
A Donut's Blog
"Bach's Prelude (Cello Suite No. 1) is driving Indiana country roads in Autumn" - Ann Kish
Omarvelous
Registered User
Posts: 232
Joined: Mon Jun 25, 2007 2:24 am

Re: converting html code to BBcode

Post by Omarvelous »

Any luck/update with this?

Really interested in this MOD....
User avatar
joeroberts
Registered User
Posts: 272
Joined: Fri Jun 22, 2007 7:19 pm

Re: converting html code to BBcode

Post by joeroberts »

Omarvelous wrote:Any luck/update with this?

Really interested in this MOD....
well this is what i got so far and it worked for a few posts so far

Code: Select all

/*------------------------------------
convert html to BBcode
--------------------------------------*/
function html_bbcode_format ($replace, $bbcode_uid) { 
$replace = str_replace(';;','',$replace);
$replace = str_replace('"','"',$replace);
$replace = str_replace('<','<',$replace);
$replace = str_replace('>','>',$replace);
$replace = str_replace('<br />','',$replace);
$replace = preg_replace('</div>', '', $replace);
$replace = preg_replace('<div align="(.*?)">', '', $replace);
$replace = preg_replace('<img src="(.*?)"(.*?) />', '[img:'.$bbcode_uid.']$1[/img:'.$bbcode_uid.']', $replace);
$replace = preg_replace('<img(.*?)src="(.*?)">', '[img:'.$bbcode_uid.']$2[/img:'.$bbcode_uid.']', $replace);
$replace = preg_replace('/\<font size=\"([1-7])\"\>((\s|.)+?)\<\/font>/i', '[size=\\1:'.$bbcode_uid.']\\2[/size:'.$bbcode_uid.']', $replace);
$replace = preg_replace('/\<font color=\"(#[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9])\"\>((\s|.)+?)\<\/font>/i', '[color=\\1:'.$bbcode_uid.']\\2[/color:'.$bbcode_uid.']', $replace);
$replace = preg_replace('/\<font color=\"([a-zA-Z]+)\]((\s|.)+?)\<\/font>/i', '[color=\\1:'.$bbcode_uid.']\\2[/color:'.$bbcode_uid.']', $replace);
$replace = preg_replace("/\<a href=\"((http|ftp|https|ftps|irc):\/\/[^<>\s]+?)\">((\s|.)+?)\<\/a\>/i","[url=\\1:'.$bbcode_uid.']\\3[/url:'.$bbcode_uid.']", $replace);
$replace = str_replace('<i>','[i:'.$bbcode_uid.']',$replace);
$replace = str_replace('</i>','[/i:'.$bbcode_uid.']',$replace);
$replace = str_replace('<b>','[b:'.$bbcode_uid.']',$replace);
$replace = str_replace('</b>','[/b:'.$bbcode_uid.']',$replace);
$replace = str_replace('<strong>','[b:'.$bbcode_uid.']',$replace);
$replace = str_replace('</strong>','[/b:'.$bbcode_uid.']',$replace);
$replace = str_replace('<u>','[u:'.$bbcode_uid.']',$replace);
$replace = str_replace('</u>','[/u:'.$bbcode_uid.']',$replace);
$replace = str_replace('<','',$replace);
$replace = str_replace('/>','',$replace);
$replace = str_replace('>','',$replace);
$replace = str_replace('&nbsp;',' ',$replace);
return $replace; 
}
/*---------------------------------------
end convert html to BBcode
-----------------------------------------*/
/*---------------------------------------
insert new topic
-----------------------------------------*/
function newtopic($name, $description, $poster)
{
global $db, $forumpx, $user, $auto_post;


$description = stripslashes($description);
        $bbcode_bitfield = "Sg==";


$bbcode_uid = substr(md5(time()), 0, 5);

        if (!array_key_exists("HTTP_X_FORWARDED_FOR",$_SERVER)) { # Proxy check. Thanks to an anonymous contributor
                $ip = $_SERVER["REMOTE_ADDR"];
        } else {
                $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        }        
if($poster == "0") $postername = 'ANONYMOUS';
else
$postername = $user->name;
if($poster == "0") $poster = "1";
else
$poster = get_user_forum_id($poster);
$replace = str_replace(";;"," ",$description);
$description = html_bbcode_format ($replace, $bbcode_uid);

#update topics
$db->sql_query("INSERT INTO `".$forumpx."_topics` (`forum_id`, `topic_title`, `topic_poster` , `topic_time` , `topic_views`, `topic_first_poster_name`, `topic_last_poster_name`, `topic_last_post_subject`, `topic_last_post_time`, `topic_last_view_time`) 
VALUES ('" . $auto_post ."', '" . $name . "', '" . $poster . "', '" . time(). "', '1', '" . $postername . "', '" . $postername . "', '" . $name . "', '" . time(). "', '" .time() . "' )");
$topicid = $db->sql_nextid();

#insert post
$db->sql_query("INSERT INTO `".$forumpx."_posts` (`topic_id`, `forum_id`, `poster_id` , `poster_ip`, `post_time`,  `post_username`, `post_subject` , `post_text` , `post_checksum`,  `post_postcount`, `bbcode_bitfield`, `bbcode_uid`) 
VALUES ('" . $topicid ."', '" . $auto_post . "', '" . $poster ."', '" . $ip . "', '" . time(). "', '" . $postername . "', '" . $name . "', '" . $description . "', '" . md5($description) ."', '1', '" . $bbcode_bitfield . "', '" . $bbcode_uid . "')");
$postid = $db->sql_nextid();



#update config
$db->sql_query("UPDATE `".$forumpx."_config` SET `config_value` = config_value  + ' 1' 
WHERE CONVERT( `".$forumpx."_config`.`config_name` USING utf8 ) = 'num_topics'  ;");

$db->sql_query("UPDATE `".$forumpx."_config` SET `config_value` = config_value  + ' 1' 
WHERE CONVERT( `".$forumpx."_config`.`config_name` USING utf8 ) = 'num_posts'  ;");

#update topic posts
$db->sql_query("INSERT INTO `".$forumpx."_topics_posted` ( `user_id` , `topic_id` , `topic_posted` )
VALUES (
'" . $poster . "', '" . $topicid ."', '1'
) ");

#update forum
$db->sql_query("UPDATE `".$forumpx."_forums` SET `forum_posts` = forum_posts + '1',
`forum_topics` = forum_topics + '1',
`forum_topics_real` = forum_topics_real + '1',
`forum_last_post_id` =  '" . $postid . "',
`forum_last_poster_id` = '" . $poster . "',
`forum_last_post_subject` = '" . $name . "',
`forum_last_post_time` = ". time() . ",
`forum_last_poster_name` = '" . $postername . "' WHERE `".$forumpx."_forums`.`forum_id` = " . $auto_post . " LIMIT 1 ;");

#if is a user update the user posts and last post time
if($poster >= "1")
{
$db->sql_query("UPDATE `".$forumpx."_users` SET `user_posts` = user_posts + '1', 
`user_lastpost_time` = " . time() ." 
WHERE `".$forumpx."_users`.`user_id` = " . $poster . "");
}
}
/*---------------------------------------
end insert new topic
-----------------------------------------*/
    
I got the bbcode_uid with

Code: Select all

$bbcode_uid = substr(md5(time()), 0, 5);   
and I used a standerd bbcode_bitfield (( Sg==)) wich turned

Code: Select all

<div align="left\"><font color=\"#ff0000\"><strong>test to see if it will work</strong></font><img alt=\"\" src=\"https://camo.phpbb.com/7ec089bc0469d659fe4e370892457a74f71367a3/687474703a2f2f7777772e6d6f76696567616d65736d6f72652e6e65742f7068704d79426974546f7272656e745f56312e322e312f68746d6c2f7468656d65732f5562756e74752f706963732f746f7272656e742e706e67\" /></div>
to

Code: Select all

[color=#ff0000:363d3][b:363d3]test to see if it will work[/b:363d3][/color:363d3][img:363d3]https://camo.phpbb.com/7ec089bc0469d659fe4e370892457a74f71367a3/687474703a2f2f7777772e6d6f76696567616d65736d6f72652e6e65742f7068704d79426974546f7272656e745f56312e322e312f68746d6c2f7468656d65732f5562756e74752f706963732f746f7272656e742e706e67[/img:363d3]   
and it looks like this as it should
:D test to see if it will workImage


now if i could figure out a way to create the proper bbcode_bitfield :roll:
gennyna
Registered User
Posts: 72
Joined: Mon Apr 06, 2009 7:29 pm

Re: converting html code to BBcode

Post by gennyna »

I too would like this change I do?
User avatar
FatherChaos
Registered User
Posts: 150
Joined: Thu Sep 26, 2002 8:23 pm
Location: Somewhere in the Chicagoland Area

Re: converting html code to BBcode

Post by FatherChaos »

yea, this would be a pimp addition to any phpbb forum
I'd definitely be using it, if it works
carsten88888
Registered User
Posts: 34
Joined: Tue Dec 30, 2014 7:22 am

Re: converting html code to BBcode

Post by carsten88888 »

I added blockquotes and bold to this script.

Code: Select all

//quote with name
		$replace = str_replace('<blockquote><div><cite>','[quote="',$replace);
		$replace = str_replace('wrote:</cite>','":'.$bbcode_uid.']',$replace);		
		//quote without name
		$replace = str_replace('<blockquote>','[quote:'.$bbcode_uid.']',$replace);
		$replace = str_replace('</blockquote>','[/quote:'.$bbcode_uid.']',$replace);
		
		$replace = preg_replace('/\<span style="font-weight: bold">((\s|.)+?)\<\/span>/i', '[b:'.$bbcode_uid.']\\1[/b:'.$bbcode_uid.']', $replace);
		
		
anyone else got updates for this script?
carsten88888
Registered User
Posts: 34
Joined: Tue Dec 30, 2014 7:22 am

Re: converting html code to BBcode

Post by carsten88888 »

I'm adding the $bbcode_uid to the bbcodes and into the row for the post. When the bbcode_uid in the post is the same as the bbcode_uid in the database column, it does no longer show the bbcode_uid in the bbcode in the frontend. But it still shows the bbcode, and its not parsing it as html.

frotnend:
Image

database:
Image

What could be wrong? What am I overlooking?
User avatar
T0ny
Registered User
Posts: 1383
Joined: Sun Jan 29, 2006 8:42 pm
Location: Lancashire
Name: Tony

Re: converting html code to BBcode

Post by T0ny »

Your bbcode_bitfield is wrong. It should be wA== to allow both bold and quote.QA== only allows bold.
carsten88888
Registered User
Posts: 34
Joined: Tue Dec 30, 2014 7:22 am

Re: converting html code to BBcode

Post by carsten88888 »

Are there more bitfield codes?
I need to allow users:
bold
img
url
quote
italic
carsten88888
Registered User
Posts: 34
Joined: Tue Dec 30, 2014 7:22 am

Re: converting html code to BBcode

Post by carsten88888 »

I've just posted a post with all the those bbcodes and got the bitfield
+YA=
in the database row. I updated all rows in the post table with that, and that seems to make everything work.

Here is the updated script I used too make html to bbcode:

Code: Select all

function html_bbcode_format($html, $bbcode_uid){ 		 
		
		$replace = str_replace(';;','',$html);
		$replace = str_replace('"','"',$replace);
		//quote with name
		$replace = str_replace('<blockquote><div><cite>','[quote="',$replace);
		$replace = str_replace(' wrote:</cite>','":'.$bbcode_uid.']',$replace);
		$replace = str_replace(' schreef:</cite>','":'.$bbcode_uid.']',$replace);//dutch
		//quote without name
		$replace = str_replace('<blockquote>','[quote:'.$bbcode_uid.']',$replace);
		$replace = str_replace('</blockquote>','[/quote:'.$bbcode_uid.']',$replace);		
		$replace = str_replace('<','<',$replace);
		$replace = str_replace('>','>',$replace);
		$replace = str_replace('<br />',"\n",$replace);
		$replace = str_replace('</div>', '', $replace);
		$replace = preg_replace('<div align="(.*?)">', '', $replace);
		$replace = preg_replace('<div>', '', $replace);
		$replace = preg_replace('/\<img src="(.*?)"(.*?) \/>/i', '[img:'.$bbcode_uid.']$1[/img:'.$bbcode_uid.']', $replace);
		$replace = preg_replace('/\<img(.*?)src="(.*?)">/i', '[img:'.$bbcode_uid.']$2[/img:'.$bbcode_uid.']', $replace);
		$replace = preg_replace('/\<font size=\"([1-7])\"\>((\s|.)+?)\<\/font>/i', '[size=\\1:'.$bbcode_uid.']\\2[/size:'.$bbcode_uid.']', $replace);
		$replace = preg_replace('/\<font color=\"(#[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9])\"\>((\s|.)+?)\<\/font>/i', '[color=\\1:'.$bbcode_uid.']\\2[/color:'.$bbcode_uid.']', $replace);
		$replace = preg_replace('/\<font color=\"([a-zA-Z]+)\]((\s|.)+?)\<\/font>/i', '[color=\\1:'.$bbcode_uid.']\\2[/color:'.$bbcode_uid.']', $replace);
		$replace = preg_replace("/\<a href=\"((http|ftp|https|ftps|irc):\/\/[^<>\s]+?)\">((\s|.)+?)\<\/a\>/i","[url=\\1:'.$bbcode_uid.']\\3[/url:'.$bbcode_uid.']", $replace);
		$replace = preg_replace('/\<span style="font-weight: bold">((\s|.)+?)\<\/span>/i', '[b:'.$bbcode_uid.']\\1[/b:'.$bbcode_uid.']', $replace);
		$replace = str_replace('<i>','[i:'.$bbcode_uid.']',$replace);
		$replace = str_replace('</i>','[/i:'.$bbcode_uid.']',$replace);
		$replace = str_replace('<b>','[b:'.$bbcode_uid.']',$replace);
		$replace = str_replace('</b>','[/b:'.$bbcode_uid.']',$replace);	
		$replace = str_replace('<strong>','[b:'.$bbcode_uid.']',$replace);
		$replace = str_replace('</strong>','[/b:'.$bbcode_uid.']',$replace);		
		$replace = str_replace('<u>','[u:'.$bbcode_uid.']',$replace);
		$replace = str_replace('</u>','[/u:'.$bbcode_uid.']',$replace);
		//$replace = str_replace('<','',$replace);
		//$replace = str_replace('/>','',$replace);
		//$replace = str_replace('>','',$replace);
		$replace = str_replace('&nbsp;',' ',$replace);
		return $replace; 
	}
	
	$bbcode_uid = substr(md5(time()), 0, 5); 
	$bbcode = html_bbcode_format($html, $bbcode_uid);
	

Return to “[3.0.x] MOD Writers Discussion”