Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function replaceNestedTagsT($text, $openTag, $closeTag, $tagName) {
- // Zjisti pocet tagu a offsety
- preg_match_all('~'.str_replace('~','\~',$openTag['search']).'~i', $text, $matchesOpen, PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER);
- preg_match_all('~'.str_replace('~','\~',$closeTag['search']).'~i', $text, $matchesClose, PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER);
- $found = array_merge($matchesOpen[0], $matchesClose[0]);
- if (empty($found))
- return $text;
- $queue = array();
- foreach ($found as $item)
- $queue[$item[1]] = $item[0];
- ksort($queue);
- // Oprav neuzavrene tagy
- $depth = 0;
- $offsetFix = 0;
- foreach($queue as $offset=>$tag) {
- $level = substr($tag,1,1) == '/' ? -1 : 1; // Koncovy nebo zacatecni tag?
- if ($depth == 0 && $level == -1) {
- $text = substr_replace($text, '', $offset-$offsetFix, $ln = strlen($tag));
- $offsetFix += $ln;
- continue;
- }
- $depth += $level;
- }
- if ($depth > 0)
- $text = substr_replace($text, str_repeat("[/$tagName]", $depth), $offset-$offsetFix+strlen($tag), 0);
- // Nahrazeni tagu HTML
- $text = preg_replace('~'.str_replace('~','\~',$openTag['search']).'~i', $openTag['replace'], $text);
- $text = preg_replace('~'.str_replace('~','\~',$closeTag['search']).'~i', $closeTag['replace'], $text);
- return $text;
- }
- function replaceNestedTagsM($text, $openTag, $closeTag)
- {
- return preg_replace_callback('~' . str_replace('~', '\~', $openTag[0]) . '(.+)' . str_replace('~', '\~', $closeTag[0]) . '~is',
- function (array $m) use ($openTag, $closeTag) {
- return sprintf($openTag[1], $m[1]) . replaceNestedTagsM($m[2], $openTag, $closeTag) . $closeTag[1];
- },
- $text
- );
- }
- function replaceNestedTagsA($text, $openTag, $closeTag) {
- $c = 0;
- do
- {
- $text = preg_replace(
- '~' . str_replace('~', '\~', $openTag[0]) . '(.+)' . str_replace('~', '\~', $closeTag[0]) . '~is',
- "$openTag[1]\\2$closeTag[1]",
- $text,
- -1,
- $c
- );
- }
- while ($c > 0);
- $text = preg_replace("~$openTag[0]|$closeTag[0]~", '', $text);
- return $text;
- }
- $text = <<<EOS
- [quote]
- [quote=Tori]
- Lorem ipsum.
- [/quote]
- Dolor sit amet.
- [/quote]
- blablabla
- EOS;
- $m = microtime(TRUE);
- for($i = 0; $i < 1e5; $i++) {
- $openTag = array('\[quote(?:=([^\]]+))?\]', '<div class="quote"><b>Citace %s</b><br>');
- $closeTag = array('\[/quote\]', '</div>');
- replaceNestedTagsM($text, $openTag, $closeTag);
- }
- echo 'Majkl: '.(microtime(TRUE) - $m);
- echo "\n";
- $a = microtime(TRUE);
- for($i = 0; $i < 1e5; $i++) {
- $openTag = array('\[quote(?:=([^\]]+))?\]', '<div class="quote"><b>Citace $1</b><br>');
- $closeTag = array('\[/quote\]', '</div>');
- replaceNestedTagsA($text, $openTag, $closeTag);
- }
- echo 'Alphard: '.(microtime(TRUE) - $a);
- echo "\n";
- $t = microtime(TRUE);
- for($i = 0; $i < 1e5; $i++) {
- $openTag = array('search'=>'\[quote(?:=([^\]]+))?\]', 'replace'=>'<div class="quote"><b>Citace $1</b><br>');
- $closeTag = array('search'=>'\[/quote\]', 'replace'=>'</div>');
- replaceNestedTagsT($text, $openTag, $closeTag, 'quote');
- }
- echo 'Tori: '.(microtime(TRUE) - $t);
- echo "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement