Advertisement
Guest User

BBCode

a guest
Aug 6th, 2011
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.19 KB | None | 0 0
  1. <?php
  2. function replaceNestedTagsT($text, $openTag, $closeTag, $tagName)   {
  3.  
  4.     // Zjisti pocet tagu a offsety
  5.     preg_match_all('~'.str_replace('~','\~',$openTag['search']).'~i', $text, $matchesOpen, PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER);
  6.     preg_match_all('~'.str_replace('~','\~',$closeTag['search']).'~i', $text, $matchesClose, PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER);
  7.     $found = array_merge($matchesOpen[0], $matchesClose[0]);
  8.     if (empty($found))
  9.         return $text;
  10.     $queue = array();
  11.     foreach ($found as $item)
  12.         $queue[$item[1]] = $item[0];
  13.     ksort($queue);
  14.    
  15.     // Oprav neuzavrene tagy
  16.     $depth = 0;
  17.     $offsetFix = 0;
  18.     foreach($queue as $offset=>$tag)    {
  19.         $level = substr($tag,1,1) == '/' ? -1 : 1; // Koncovy nebo zacatecni tag?
  20.         if ($depth == 0 && $level == -1)    {
  21.             $text = substr_replace($text, '', $offset-$offsetFix, $ln = strlen($tag));
  22.             $offsetFix += $ln;
  23.             continue;
  24.         }
  25.         $depth += $level;
  26.     }
  27.     if ($depth > 0)
  28.         $text = substr_replace($text, str_repeat("[/$tagName]", $depth), $offset-$offsetFix+strlen($tag), 0);
  29.        
  30.     // Nahrazeni tagu HTML
  31.     $text = preg_replace('~'.str_replace('~','\~',$openTag['search']).'~i', $openTag['replace'], $text);
  32.     $text = preg_replace('~'.str_replace('~','\~',$closeTag['search']).'~i', $closeTag['replace'], $text);
  33.     return $text;
  34. }
  35.  
  36. function replaceNestedTagsM($text, $openTag, $closeTag)
  37. {
  38.     return preg_replace_callback('~' . str_replace('~', '\~', $openTag[0]) . '(.+)' . str_replace('~', '\~', $closeTag[0]) . '~is',
  39.         function (array $m) use ($openTag, $closeTag) {
  40.             return sprintf($openTag[1], $m[1]) . replaceNestedTagsM($m[2], $openTag, $closeTag) . $closeTag[1];
  41.         },
  42.         $text
  43.     );
  44. }
  45.  
  46. function replaceNestedTagsA($text, $openTag, $closeTag)    {
  47.  
  48.     $c = 0;
  49.     do
  50.     {
  51.         $text = preg_replace(
  52.             '~' . str_replace('~', '\~', $openTag[0]) . '(.+)' . str_replace('~', '\~', $closeTag[0]) . '~is',
  53.             "$openTag[1]\\2$closeTag[1]",
  54.             $text,
  55.             -1,
  56.             $c
  57.         );
  58.     }
  59.     while ($c > 0);
  60.     $text = preg_replace("~$openTag[0]|$closeTag[0]~", '', $text);
  61.     return $text;
  62. }
  63.  
  64. $text = <<<EOS
  65. [quote]
  66.     [quote=Tori]
  67.         Lorem ipsum.
  68.     [/quote]
  69.     Dolor sit amet.
  70. [/quote]
  71. blablabla
  72. EOS;
  73.  
  74.  
  75. $m = microtime(TRUE);
  76. for($i = 0; $i < 1e5; $i++) {
  77.     $openTag = array('\[quote(?:=([^\]]+))?\]', '<div class="quote"><b>Citace %s</b><br>');
  78.     $closeTag = array('\[/quote\]', '</div>');
  79.     replaceNestedTagsM($text, $openTag, $closeTag);
  80. }
  81. echo 'Majkl: '.(microtime(TRUE) - $m);
  82. echo "\n";
  83.  
  84. $a = microtime(TRUE);
  85. for($i = 0; $i < 1e5; $i++) {
  86.     $openTag = array('\[quote(?:=([^\]]+))?\]', '<div class="quote"><b>Citace $1</b><br>');
  87.     $closeTag = array('\[/quote\]', '</div>');
  88.     replaceNestedTagsA($text, $openTag, $closeTag);
  89. }
  90. echo 'Alphard: '.(microtime(TRUE) - $a);
  91. echo "\n";
  92.  
  93.  
  94. $t = microtime(TRUE);
  95. for($i = 0; $i < 1e5; $i++) {
  96.     $openTag = array('search'=>'\[quote(?:=([^\]]+))?\]', 'replace'=>'<div class="quote"><b>Citace $1</b><br>');
  97.     $closeTag = array('search'=>'\[/quote\]', 'replace'=>'</div>');
  98.     replaceNestedTagsT($text, $openTag, $closeTag, 'quote');
  99. }
  100. echo 'Tori: '.(microtime(TRUE) - $t);
  101. echo "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement