int regexp(char *exp, char *str)
{
short cnt;
for ( ; *exp != 0 ; exp++, str++)
{
switch (*exp)
{
case '?':
if (*str == 0)
{
return FALSE;
}
break;
case '*':
for (cnt = 0 ; str[cnt] ; cnt++)
{
if (regexp(exp + 1, &str[cnt]))
{
// copy cnt chars to string or something
return TRUE;
}
}
return FALSE;
case '\\':
if (*++exp != *str)
{
return FALSE;
default:
if (tolower(*exp) != tolower(*str))
{
return FALSE;
}
break;
}
}
return (*exp == *str);
}
<?php
$var = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
function regexp($exp, $str)
{
global $var;
$num = 0;
$cnt = 0;
$tnc = 0;
while ($exp[$cnt])
{
switch ($exp[$cnt])
{
case '%':
$num = $exp[$cnt+1];
for ($len = strlen($str) ; $len >= $tnc ; $tnc++)
{
if (regexp(substr($exp, $cnt+2), substr($str, $tnc)) == "1")
{
$var[$num] = substr($str, $cnt, $tnc - $cnt);
return "1";
}
}
return "0";
case '\\':
if (strtolower($exp[$cnt]) != strtolower($str[$tnc]))
{
return "0";
}
break;
default:
if (strtolower($exp[$cnt]) != strtolower($str[$tnc]))
{
return "0";
}
break;
}
$cnt++;
$tnc++;
}
return (strtolower($exp[$cnt]) == strtolower($str[$tnc]) ? "1" : "0");
}
function substitute($input)
{
global $var;
$output = "";
for ($cnt = 0 ; $input[$cnt] ; $cnt++)
{
if ($input[$cnt] == "%")
{
$cnt++;
$num = $input[$cnt];
$output = $output . $var[$num];
}
else
{
$output = $output . $input[$cnt];
}
}
return $output;
}
if (regexp("[font='%1']%2[/font]", "[font='test']hiyas[/font]"))
{
echo "%1: $var[1] %2:$var[2]<br>";
echo "substitute: " . substitute("[bla='%1']%2[/bla]");
}
else
{
echo "no match";
}
?>
<?php
$bbcodes = array(
array("[b]", "[/b]", "[b]%5[/b]", "<b>%5</b>"),
array("", "<b>%5</b>"),
array("[font size", "[/font]", "[font size='%0']%5[/font]", "<font size='%0'>%5</font>"),
array("", "", "", "")
);
$var = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
function match($exp, $str)
{
global $var;
$expcnt = 0;
$strcnt = 0;
while ($expcnt < strlen($exp))
{
switch ($exp[$expcnt])
{
case '%':
$num = $exp[$expcnt+1];
if ($num <= 5)
{
for ($strlen = $strcnt ; $strlen < strlen($str) ; $strlen++)
{
if (match(substr($exp, $expcnt+2), substr($str, $strlen)) == "1")
{
$var[$num] = substr($str, $strcnt, $strlen - $strcnt);
return "1";
}
}
}
else
{
for ($strlen = strlen($str) ; $strlen >= $strcnt ; $strlen–)
{
if (match(substr($exp, $expcnt+2), substr($str, $strlen)) == "1")
{
$var[$num] = substr($str, $strcnt, $strlen - $strcnt);
return "1";
}
}
}
return "0";
default:
if (strcasecmp(substr($exp, $expcnt, 1), substr($str, $strcnt, 1)))
{
return "0";
}
break;
}
$expcnt++;
$strcnt++;
}
if (strlen($exp) == strlen($str))
{
return "1";
}
else
{
return "0";
}
}
function substitute($input)
{
global $var;
$output = "";
for ($cnt = 0 ; $cnt < strlen($input) ; $cnt++)
{
if ($input[$cnt] == "%")
{
$cnt++;
$num = $input[$cnt];
$output = $output . $var[$num];
}
else
{
$output = $output . $input[$cnt];
}
}
return $output;
}
function bblicious($text)
{
global $bbcodes;
$level = 0;
for ($ind = 0 ; $bbcodes[$ind][0] ; $ind++)
{
if (strncasecmp($text, $bbcodes[$ind][0], strlen($bbcodes[$ind][0])) == 0)
{
break;
}
}
if (strcmp($bbcodes[$ind][0], "") == 0)
{
return "error";
}
for ($cnt = 0 ; $cnt < strlen($text) ; $cnt++)
{
if (strncasecmp(substr($text, $cnt), $bbcodes[$ind][0], strlen($bbcodes[$ind][0])) == 0)
{
$level++;
}
else if (!strncasecmp(substr($text, $cnt), $bbcodes[$ind][1], strlen($bbcodes[$ind][1])))
{
$level–;
if ($level == 0)
{
$cnt += strlen($bbcodes[$ind][1]);
break;
}
}
}
if ($level != 0)
{
return "error";
}
if (match($bbcodes[$ind][2], substr($text, 0, $cnt)))
{
return substitute($bbcodes[$ind][3]) . substr($text, $cnt);
}
else
{
return "error";
}
}
function bbfication($text)
{
$cnt = 1;
for ($cnt = 0 ; $cnt < strlen($text) ; $cnt++)
{
if ($text[$cnt] == "[")
{
if (strcmp(bblicious(substr($text, $cnt)), "error"))
{
$text = substr($text, 0, $cnt) . bblicious(substr($text, $cnt));
$cnt–;
}
}
}
return $text;
}
echo "[B]hello[/b]: " . bbfication("[b]hello[/b]") . "<br>";
echo "[fOnt siZe='6']Hello[/font]: " . bbfication("[font size='6']Hello[/font]") . "<br>";
echo "[fOnt siZe='6'][b]hello[/b][/font]: " . bbfication("[font size='6'][b]hello[/b][/font]") . "<br>";
echo "[font size='6']he[font size='3']ll[/font]o[/font]: " . bbfication("[font size='6']he[font size='3']ll[/font]o[/font]") . "<br>";
?>
[/code]
[quote=[url=http://www.google.ca]Google[/url]]
We own the internet
[/quote]
I'm trying to come up with some code that will allow site administrators to create custom bbcode tags for their sites. I figure I need basically this:
* DB table to store them in. With the bbcode tag name and HTML translation of the tag. This is easy, I created it inside of 30 seconds :)
* A way for users to input their tag names, eg: text[/blah]. Stuck on this. Not sure how to gather the input from the user.
* Second input area to take the HTML translation, eg: <font face="$1">$2</font> where $1 is the font name, $2 is user text. This part I think I can handle ok. The person inputting the code is going to need to know to format it that way anyway.
* A function to query the bbcode table and then go over them and translate as needed. Again, pretty easy stuff.
So if someone could lend a hand on the second part. If it was a simple tag like text I could just have the user input the "b" and let the code assume the rest during translation. My problem comes in wanting to enter stuff like the font tag that you need a tag to specify what font you want. My guess would be this requires regex, in which case I'm going to be in a heap of trouble since I don't understand it.
I'd also rather avoid digging through code in another forum package to figure it out.