function fixup_quotes($string) { // skip the whole thing when there are no quotes if (!(strchr($string, '[quote]') || strchr($string, '[/quote]'))) { return $string; } $tmp = $string; $s_pos = 0; // $s_pos is position in $string $t_pos = 0; // $t_pos is position in $tmp $tags = new CStack(); while (is_integer($t_pos = strpos($tmp, '['))) { $s_pos += $t_pos + 1; $tmp = substr($tmp, $t_pos + 1); $endpos = strpos($tmp, ']'); if (is_integer($endpos)) { $curtag = substr($tmp, 0, $endpos); switch ($curtag) { case 'quote': $tags->push($curtag); break; case '/quote': close_tags($tags, $s_pos, $string, substr($curtag, 1)); break; default: //ttt: don't be fooled by [[quote] stuff $endpos = -1; break; } $s_pos += $endpos + 1; $tmp = substr($string, $s_pos); } } // if there are still some endtags missing, add them at the end $s_pos = strlen($string) + 1; // normally this should be -1, but close_tags moves back 2 chars close_tags($tags, $s_pos, $string); return $string; }
public function testPush() { $stack = new CStack(); $stack->push(1); $this->assertEquals(array(1), $stack->toArray()); }