protected function build_sub_quotations() { global $__ax, $__ay; $exp = strpos($this->_text, "</cA===>") !== false ? "</cA===>" : (strpos($this->_text, "\r\n") !== false ? "\r\n\r\n" : "\n\n"); $texts_in = explode($exp, $this->_text); $texts_out = array(); foreach ($texts_in as $textx) { $okposstack = array('0'); $okpos = 0; $level = 0; $off = 0; while (true) { $p = EMTLib::strpos_ex($textx, 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, $textx); } } $level++; } if ($p['str'] == "»") { $level--; if ($level > 0) { if (!$this->is_on('no_bdquotes')) { $this->inject_in($p['pos'], self::QUOTE_CRAWSE_CLOSE, $textx); } } } $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($textx, $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]+)\»\;/ui", '\1\2″', $k, 1, $amount); $amount = 0; $__ax = preg_match_all("/(^|[^0-9])([0-9]+)\\»\\;/ui", $k, $m); $__ay = 0; if ($__ax) { $k = preg_replace_callback("/(^|[^0-9])([0-9]+)\\»\\;/ui", create_function('$m', 'global $__ax,$__ay; $__ay++; if($__ay==$__ax){ return $m[1].$m[2]."″";} return $m[0];'), $k); $amount = 1; } } while ($amount == 0 && count($okposstack)); // успешно сделали замену if ($amount == 1) { // заново просмотрим содержимое $textx = substr($textx, 0, $lokpos) . $k . substr($textx, $off); $off = $lokpos; $level = 0; continue; } // иначе просто заменим последнюю явно на " от отчаяния if ($amount == 0) { // говорим, что всё в порядке $level = 0; $textx = substr($textx, 0, $p['pos']) . '"' . substr($textx, $off); $off = $p['pos'] + strlen('"'); $okposstack = array($off); continue; } } } } // не совпало количество, отменяем все подкавычки if ($level != 0) { // закрывающих меньше, чем надо if ($level > 0) { $k = substr($textx, $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); $textx = substr($textx, 0, $okpos) . $k; } } $texts_out[] = $textx; } $this->_text = implode($exp, $texts_out); }