PDA

View Full Version : Allow x nested quotes


Boots
01-21-2005, 03:26 PM
how can I modify this function to allow x amount of nested quotes?


// ###################### Start strip_quotes #######################
function strip_quotes($text)
{
$lowertext = strtolower($text);

// find all = 'start';
$curpos = $pos + 6;
}
}
while ($pos !== false);

if (sizeof($start_pos) == 0)
{
return $text;
}

// find all tags
$end_pos = array();
$curpos = 0;
do
{
$pos = strpos($lowertext, '[/quote]', $curpos);
if ($pos !== false)
{
$end_pos["$pos"] = 'end';
$curpos = $pos + 8;
}
}
while ($pos !== false);

if (sizeof($end_pos) == 0)
{
return $text;
}

// merge them together and sort based on position in string
$pos_list = $start_pos + $end_pos;
ksort($pos_list);

do
{
// build a stack that represents when a quote tag is opened
// and add non-quote text to the new string
$stack = array();
$newtext = '';
$substr_pos = 0;
foreach ($pos_list AS $pos => $type)
{
$stacksize = sizeof($stack);
if ($type == 'start')
{
// empty stack, so add from the last close tag or the beginning of the string
if ($stacksize == 0)
{
$newtext .= substr($text, $substr_pos, $pos - $substr_pos);
}
array_push($stack, $pos);
}
else
{
// pop off the latest opened tag
if ($stacksize)
{
array_pop($stack);
$substr_pos = $pos + 8;
}
}
}

// add any trailing text
$newtext .= substr($text, $substr_pos);

// check to see if there's a stack remaining, remove those points
// as key points, and repeat. Allows emulation of a non-greedy-type
// recursion.
if ($stack)
{
foreach ($stack AS $pos)
{
unset($pos_list["$pos"]);
}
}
}
while ($stack);

return $newtext;
}


would this work?


// ###################### Start strip_quotes #######################
function strip_quotes($text)
{
$nestlevel = 3;
$lowertext = strtolower($text);

// find all = 'start';
$curpos = $pos + 6;
$nestcount++;
}
}
while (($pos !== false) && ($nestcount < $nestlevel));

if (sizeof($start_pos) == 0)
{
return $text;
}

// find all tags
$end_pos = array();
$curpos = 0;
$nestcount = 0;
do
{
$pos = strpos($lowertext, '[/quote]', $curpos);
if ($pos !== false)
{
$end_pos["$pos"] = 'end';
$curpos = $pos + 8;
$nestcount++;
}
}
while (($pos !== false) && ($nestcount < $nestlevel));

if (sizeof($end_pos) == 0)
{
return $text;
}

// merge them together and sort based on position in string
$pos_list = $start_pos + $end_pos;
ksort($pos_list);

do
{
// build a stack that represents when a quote tag is opened
// and add non-quote text to the new string
$stack = array();
$newtext = '';
$substr_pos = 0;
foreach ($pos_list AS $pos => $type)
{
$stacksize = sizeof($stack);
if ($type == 'start')
{
// empty stack, so add from the last close tag or the beginning of the string
if ($stacksize == 0)
{
$newtext .= substr($text, $substr_pos, $pos - $substr_pos);
}
array_push($stack, $pos);
}
else
{
// pop off the latest opened tag
if ($stacksize)
{
array_pop($stack);
$substr_pos = $pos + 8;
}
}
}

// add any trailing text
$newtext .= substr($text, $substr_pos);

// check to see if there's a stack remaining, remove those points
// as key points, and repeat. Allows emulation of a non-greedy-type
// recursion.
if ($stack)
{
foreach ($stack AS $pos)
{
unset($pos_list["$pos"]);
}
}
}
while ($stack);

return $newtext;
}