function Mysubstr ($STR, $length) {
$tagcnt = 0; $charcnt = 0; $tag = '; $maxlen = strlen ($STR); $resultstr = '; $tagstack = Array (); for ($i = 0; $i < $length; $i + +) { if ($str [$i] = = ' < ') { $resultstr. = $str [$i]; for ($j = $i; $str [$j]!= ' > '; $j + +, $length + +) { $tag. = $str [$j]; } $tagcnt + +; $length + +; $tag. = ' > ';
If the start tag, then the stack, if it is corresponding to the end tag is out of the stack if (Preg_match ('/< ([^/]+)? >/i ', $tag, $r)) { echo ' into stack: ', Htmlspecialchars ($r [1]), ' <br/> '; Array_push ($tagstack, $r [1]); } ElseIf (Preg_match ('/'. $tagstack [Count ($tagstack)-1]. ' /', $tag)) { Echo ' Out stack: ', Htmlspecialchars ($tagstack [Count ($tagstack)-1]), ' <br/> '; Array_pop ($tagstack); } $tag = '; Continue } $charcnt + +; $resultstr. = $str [$i]; } The final result of Echo ' Stack is empty direct return if (empty ($tagstack)) { return $resultstr; } Otherwise remove the start tag without the end tag else{
while (!empty ($tagstack)) { $tag = Array_pop ($tagstack); $index = Strrpos ($resultstr, $tag); for ($i = $index-1; $resultstr [$i]!= ' > '; $i + +) { $RESULTSTR [$i] = '; } $resultstr [$i + +] = ';
} return $resultstr; }
} $sttime = Microtime (true); $stmem = Memory_get_usage (); $str = "A1<body>b2<p>c3<em>d4</em>e5</p>f6</body>g7h8"; echo ' processing results are: <br/> echo "Memory Usage:" (Memory_get_usage ()-$stmem), ' <br/> '; echo "Algorithm run Time (microtime):" (Microtime (True)-$sttime), ' <br/> '; |