コード例 #1
0
ファイル: Quote.php プロジェクト: kix/mdash
 protected function build_sub_quotations()
 {
     global $__ax, $__ay;
     $okposstack = array('0');
     $okpos = 0;
     $level = 0;
     $off = 0;
     while (true) {
         $p = Util::strpos_ex($this->_text, array("«", "»"), $off);
         if ($p === false) {
             break;
         }
         if ($p['str'] == "«") {
             if ($level > 0) {
                 if (!$this->is_on('no_bdquotes')) {
                     $this->inject_in($p['pos'], self::QUOTE_CRAWSE_OPEN);
                 }
             }
             $level++;
         }
         if ($p['str'] == "»") {
             $level--;
             if ($level > 0) {
                 if (!$this->is_on('no_bdquotes')) {
                     $this->inject_in($p['pos'], self::QUOTE_CRAWSE_CLOSE);
                 }
             }
         }
         $off = $p['pos'] + strlen($p['str']);
         if ($level == 0) {
             $okpos = $off;
             array_push($okposstack, $okpos);
         } elseif ($level < 0) {
             // уровень стал меньше нуля
             if (!$this->is_on('no_inches')) {
                 do {
                     $lokpos = array_pop($okposstack);
                     $k = substr($this->_text, $lokpos, $off - $lokpos);
                     $k = str_replace(self::QUOTE_CRAWSE_OPEN, self::QUOTE_FIRS_OPEN, $k);
                     $k = str_replace(self::QUOTE_CRAWSE_CLOSE, self::QUOTE_FIRS_CLOSE, $k);
                     //$k = preg_replace("/(^|[^0-9])([0-9]+)\&raquo\;/ui", '\1\2&Prime;', $k, 1, $amount);
                     $amount = 0;
                     $__ax = preg_match_all("/(^|[^0-9])([0-9]+)\\&raquo\\;/ui", $k, $m);
                     $__ay = 0;
                     if ($__ax) {
                         $k = preg_replace_callback("/(^|[^0-9])([0-9]+)\\&raquo\\;/ui", create_function('$m', 'global $__ax,$__ay; $__ay++; if ($__ay==$__ax) { return $m[1].$m[2]."&Prime;";} return $m[0];'), $k);
                         $amount = 1;
                     }
                 } while ($amount == 0 && count($okposstack));
                 // успешно сделали замену
                 if ($amount == 1) {
                     // заново просмотрим содержимое
                     $this->_text = substr($this->_text, 0, $lokpos) . $k . substr($this->_text, $off);
                     $off = $lokpos;
                     $level = 0;
                     continue;
                 }
                 // иначе просто заменим последнюю явно на &quot; от отчаяния
                 if ($amount == 0) {
                     // говорим, что всё в порядке
                     $level = 0;
                     $this->_text = substr($this->_text, 0, $p['pos']) . '&quot;' . substr($this->_text, $off);
                     $off = $p['pos'] + strlen('&quot;');
                     $okposstack = array($off);
                     continue;
                 }
             }
         }
     }
     // не совпало количество, отменяем все подкавычки
     if ($level != 0) {
         // закрывающих меньше, чем надо
         if ($level > 0) {
             $k = substr($this->_text, $okpos);
             $k = str_replace(self::QUOTE_CRAWSE_OPEN, self::QUOTE_FIRS_OPEN, $k);
             $k = str_replace(self::QUOTE_CRAWSE_CLOSE, self::QUOTE_FIRS_CLOSE, $k);
             $this->_text = substr($this->_text, 0, $okpos) . $k;
         }
     }
 }