Quantcast

Jump to content

» «
Photo

[WIP][PHP] BBcode HTML convertor

1 reply to this topic
Svip
  • Svip

    I eat babies

  • The Connection
  • Joined: 12 Nov 2001
  • None

#1

Posted 05 May 2005 - 06:29 PM

sviip.dk/stuff/bbcoder.php

Okay. This convertor converts HTML to BBcode, so if you have copied a page and want to make it able for people to view on here, but you know that these forums doesn't support HTML, you would like it converted to BBcode.

This page is your change.

It should also be possible to go the other way around, but not yet though.

However, here is the code:
CODE
<?php
function fix_tablecode($text) {
return '[table]'.preg_replace('#\<tr(.+?)(.+?)\</tr\>#ies', "fix_trcode('\\2')", $text).'[/table]';
}
function fix_trcode($text) {
$find = array(
 '@\<td(.*?)\>(.*?)\</td\>@is',
 '@\<th(.*?)\>(.*?)\</th\>@is',
);
$replace = array(
 '[td]$2[/td]',
 '[th]$2[/th]',
);
return '[tr]'.preg_replace($find, $replace, $text).'[/tr]';
}
if ($_POST['bthsubmit']) {
$text = str_replace('\\', '', $_POST['htmlcode']);
$find = array(
 '@\<b\>(.*?)\</b\>@is',
 '@\<i\>(.*?)\</i\>@is',
 '@\<a href\=\"(.*?)\"(.*?)\>(.*?)\</a\>@is',
 '#\<table(.+?)(.+?)\</table\>#ies',
 '@\<font(.*?)color\=\"(.*?)\"(.*?)\>(.*?)\</font\>@is',
 '@\<span(.*?)style\=\"(.*?)color:(.*?)(.*?)\"(.*?)\>(.*?)\</span\>@is',
);
$replace = array(
 '[b]$1[/b]',
 '[i]$1[/i]',
 '[url=$1]$3[/url]',
 "fix_tablecode('\\2')",
 '[color=$2]$4[/color]',
 '[color=$3]$6[/color]',
);
$bbcode = preg_replace($find, $replace, $text);
}
elseif ($_POST['htbsubmit']) {

}
$form = '<form action="bbcoder.php" method="post"><div><label for="htmlcode">HTML input/output:</label><br /><textarea name="htmlcode" id="htmlcode" cols="97%" rows="15">'.$htmlcode.'</textarea><br /><br /><span><input type="submit" value="HTML 2 BBcode" name="bthsubmit"><input type="submit" value="BBcode 2 HTML" name="htbsubmit"></span><br /><label for="bbcode">BBCode input/output:</label><br /><textarea name="bbcode" id="bbcode" cols="97%" rows="15">'.$bbcode.'</textarea>';
$html = $form;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/ xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>HTML BBCode convertor</title>
</head>
<body align="center">
<?= $html ?>
</body>
</html>


Remember; it's still WIP

Johnno
  • Johnno

    WD&P Ogre

  • Members
  • Joined: 15 Sep 2002

#2

Posted 05 May 2005 - 11:39 PM

Reminds me of one I started writing a while ago. Only mine converted between both tounge.gif




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users