<?php class Quote_Parser { private $text; public function __construct($text) { $this->text = $text; } public function set_tags($openning, $closing) { } public function get_tags() { return $this->tags; } public function set_text($text) { $this->text = $text; } public function set_list($positions_list, $key2position, $list, $sections, $sections_position) { } public function get_list() { return $this->list; } public function parse() { $this->_create_list_from_text(); $text_section = $this->_analyse_list(); $parsed_text = $this->_generate_text($text_section); return $parsed_text; } private function _create_list_from_text() { list($openning, $closing) = $this->get_tags(); $openning_positions = $this->_get_all_occurrences($this->text, $openning); $closing_positions = $this->_get_all_occurrences($this->text, $closing); $positions_list = array_combine($openning_positions, array_fill(0, count($openning_positions), 'o')); $positions_list += array_combine($closing_positions, array_fill(0, count($closing_positions), 'c')); $key2position = array_keys($positions_list); // lista zawiera powiazanie pomiedzy kluczem glownym, a pozycyjnym $openning_count = $closing_count = $current_section = $last_key = 0; foreach($list as $key => $tag) { if ($tag == 'o') { $openning_count++; } } elseif ($tag == 'c') { $closing_count++; if ($openning_count == $closing_count) { $sections_position[$current_section]['end'] = $key2position[$key]; $openning_count = $closing_count = 0; for($i = $last_key; $i <= $key; $i++) { $sections[$i] = $current_section; } $last_key = $key + 1; $current_section++; } } } $this->set_list($positions_list, $key2position, $list, $sections, $sections_position); } private function _analyse_list() { $text = $this->text; $tmp_text = ''; list($openning_tag, $closing_tag) = $this->get_tags(); $closing_tag_length = mb_strlen($closing_tag); list($positions_list, $key2position, $list, $sections, $sections_position) = $this->get_list(); $openning_key = $closing_key - 1; $closing_position = $key2position[$closing_key]; $openning_position = $key2position[$openning_key]; $length = $closing_position - $openning_position; $inner_text = mb_substr($text, $openning_position, $length + $closing_tag_length); $text = mb_substr($text, 0, $openning_position) . mb_substr($text, $closing_position + $closing_tag_length); $text_section[$sections[$closing_key]][] = $inner_text; for ($i = $closing_key + 1; $i <$list_length; $i++) { $key2position[$i]-=$length + $closing_tag_length; } } return $text_section; } private function _generate_text($text_section) { list($positions_list, $key2position, $list, $sections, $sections_position) = $this->get_list(); list($openning_tag, $closing_tag) = $this->get_tags(); $closing_tag_length = mb_strlen($closing_tag); $text = $this->text; for ($i = 0; $i < $sections_position_length; $i++) { $replacement_string = '[section='.$i.']'; $replacement_string_length = mb_strlen($replacement_string); $text = mb_substr($text, 0, $sections_position[$i]['start']).$replacement_string.mb_substr($text, $sections_position[$i]['end'] + $closing_tag_length); $difference = $sections_position[$i]['end'] + $closing_tag_length - $sections_position[$i]['start'] - $replacement_string_length; for ($j = $i + 1; $j < $sections_position_length; $j++) { $sections_position[$j]['start'] -= $difference; $sections_position[$j]['end'] -= $difference; } } for ($i = 0; $i < $sections_length; $i++) { } return $text; } private function _get_all_occurrences($haystack, $needle, $offset = 0) { for($i = $offset; $i < mb_strlen($haystack); $i++) { if ( ($pos = mb_strpos($haystack, $needle, $i)) !== FALSE) { $offset = $pos; if ($offset >= $i) { $i = $offset; $result[] = $offset; } } } return $result; } }
Niestety potrzebuję małej poprawki, która zmieni kolejność odpowiedzi.
Przykład tutaj: http://smokeit.pl/post/zobacz/14-test-obrazkw-z-linku
Trzeba nacisnąć:"Wyświetl komentarze"
Spójrzcie na komentarz na samej górze. Chciałbym aby to miało inną postać, a mianowicie taką:
Testowy komentarz
Fajny ten testowy komentarz
Wiem, dzięki
Nie ma sprawy
Czyli te 3 odpowiedzi, aby miały odwrotną kolejność od obecnej, co należy zmienić w tym kodzie?
P.S. https://brokers-star.com/bbcode/ <-testowanie obecnej wersji kodu