Lets say I have this text....
I have replaced * ] => ) and [ => ( *, because forum want to convert my example
Code: Select all
$str = "(quote)mmmmmmmmmmmmmmm(color=#009977)hi there(/color)
(/quote) guess who (email)pop@you.com(/email)
(quote)mmmmmmmmmmmmmmm(color=#009977)hi there(/color)(/quote)
(email)pop@you.com(/email) guess who
(email)pop@you.com(/email)
(quote)mmmmmmmmmmmmmmm(color=#009977)hi there(/color)(/quote)
hi there (i)water(/i) bye
";Code: Select all
<!-- /s(quote)mmmmmmmmmmmmmmm(color=#009977)hi there(/color)
(/quote)/s --> guess who <!-- /s(email)pop@you.com(/email)/s -->
<!-- /s(quote)mmmmmmmmmmmmmmm(color=#009977)hi there(/color)(/quote)/s -->
<!-- /s(email)pop@you.com(/email)/s --> guess who
<!-- /s(email)pop@you.com(/email)/s -->
<!-- /s(quote)mmmmmmmmmmmmmmm(color=#009977)hi there(/color)(/quote)/s -->
hi there <!-- /s(i)water(/i)/s --> byeIf the user has selected to not spell check any BBCODE
Should I use a regex or just a simple loop? I have found a really long example (so much code) that works and seems fast. But I guess I need to not be lazy and reinvent the wheel.
Here is the example I found...
Code: Select all
<?
$str = '';
$s = '<!-- /s';
$e = '/s -->';
$out = sc ( $str, $s, $e );
echo $out;
function sc ( $text, $es, $ee )
{
$bb = array ( 'q' => array ( array ( 5, 'quote' ) ), 'r' => array ( array ( 5, 'right' ) ), 'e' => array ( array ( 5, 'email' ) ), 'h' => array ( array ( 4, 'html' ) ), 'p' => array ( array ( 3, 'php' ) ), 'f' => array ( array ( 4, 'font' ) ), 'l' => array ( array ( 4, 'list' ), array ( 4, 'left' ) ), 's' => array ( array ( 4, 'size' ) ), 'b' => array ( array ( 5, 'bible' ), array ( 2, 'b]', 'b' ) ), 'u' => array ( array ( 3, 'url' ), array ( 2, 'u]', 'u' ) ), 'c' => array ( array ( 4, 'code' ), array ( 5, 'color' ), array ( 6, 'center' ) ), 'i' => array ( array ( 3, 'img' ), array ( 6, 'indent' ), array ( 2, 'i]', 'i' ) ) );
$su = 'ABCDEFGHILMNOPQRSTUZ';
$sl = 'abcdefghilmnopqrstuz';
$os = '';
$sp = 0;
$oa = array ();
$ls = strtr ( $text, $su, $sl );
while ( ( $ep = strpos ( $ls, '[' ) ) !== false )
{
$os .= substr ( $text, $sp, $ep );
$ls = substr ( $ls, $ep );
$sp += $ep;
$on = '';
$ad = '';
$oc = 0;
$ct = 1;
$st = $ls{1};
if ( ! empty ( $oa ) )
{
$on = $oa[0];
$oc = $oa[1];
$oa = array ();
}
if ( $st == '/' )
{
$ct++;
$st = $ls{2};
}
if ( isset ( $bb[$st] ) )
{
foreach ( $bb[$st] AS $cc )
{
if ( substr ( $ls, $ct, $cc[0] ) == $cc[1] )
{
if ( isset ( $cc[2] ) )
{
$cc = array ( 1, $cc[2] );
$ad = ']';
}
$oa[0] = $cc[1];
break;
}
}
if ( ! empty ( $oa ) )
{
$oa[1] = 1;
if ( $on == '' )
{
$to = substr_count ( $ls, '[' . $oa[0] . $ad );
$tc = substr_count ( $ls, '[/' . $oa[0] . ']' );
if ( $to == 0 || $tc == 0 )
{
$os .= '[';
$ls = substr ( $ls, 1 );
$sp += 1;
$oa = array ();
}
else
{
if ( $to > $tc )
{
$oa[1] -= $to - $tc;
}
else if ( $to < $tc )
{
$oa[1] += $tc - $to;
}
$ts = $cc[0] + 1;
$os .= $es . substr ( $text, $sp, $ts );
$ls = substr ( $ls, $ts );
$sp += $ts;
}
}
else
{
if ( $ct == 2 && $on == $oa[0] && $oc == $oa[1] )
{
$ts = $cc[0] + 3;
$os .= substr ( $text, $sp, $ts ) . $ee;
$ls = substr ( $ls, $ts );
$sp += $ts;
$oa = array ();
}
else
{
$os .= '[';
$ls = substr ( $ls, 1 );
$sp += 1;
if ( $ct == 2 && $on == $oa[0] && $oc > $oa[1] )
{
$oa[1] = $oc - 1;
}
else if ( $ct == 1 && $on == $oa[0] )
{
$oa[1] += $oc;
}
else
{
$oa = array ( $on, $oc );
}
}
}
}
else
{
$oa = $on == '' ? null : array ( $on, $oc );
$os .= '[';
$ls = substr ( $ls, 1 );
$sp += 1;
}
}
else
{
$oa = $on == '' ? null : array ( $on, $oc );
$os .= '[';
$ls = substr ( $ls, 1 );
$sp += 1;
}
}
$os .= $ls == '' ? '' : substr ( $text, $sp );
return ( $os );
}
?>Thanks for reading...
yj