Witam, posiadam kod, który rozdziela mi cytaty, wstawiane na moją stronę:
  1. <?php
  2. class Quote_Parser {
  3. private $list = array();
  4. private $tags = array();
  5. private $text;
  6.  
  7. public function __construct($text) {
  8. $this->text = $text;
  9. }
  10.  
  11. public function set_tags($openning, $closing) {
  12. $this->tags = array($openning, $closing);
  13. }
  14.  
  15. public function get_tags() {
  16. return $this->tags;
  17. }
  18.  
  19. public function set_text($text) {
  20. $this->text = $text;
  21. }
  22.  
  23. public function set_list($positions_list, $key2position, $list, $sections, $sections_position) {
  24. $this->list = array($positions_list, $key2position, $list, $sections, $sections_position);
  25. }
  26.  
  27. public function get_list() {
  28. return $this->list;
  29. }
  30.  
  31. public function parse() {
  32. $this->_create_list_from_text();
  33. $text_section = $this->_analyse_list();
  34. $parsed_text = $this->_generate_text($text_section);
  35. return $parsed_text;
  36. }
  37.  
  38. private function _create_list_from_text() {
  39. list($openning, $closing) = $this->get_tags();
  40. $openning_positions = $this->_get_all_occurrences($this->text, $openning);
  41. $closing_positions = $this->_get_all_occurrences($this->text, $closing);
  42. $positions_list = array_combine($openning_positions, array_fill(0, count($openning_positions), 'o'));
  43. $positions_list += array_combine($closing_positions, array_fill(0, count($closing_positions), 'c'));
  44. ksort($positions_list); // lista zawiera pozycje znacznikoww tekscie
  45. $key2position = array_keys($positions_list); // lista zawiera powiazanie pomiedzy kluczem glownym, a pozycyjnym
  46. $list = array_merge($positions_list); // zawiera liste z kolejnoscia poszczegolnych znacznikow
  47. $openning_count = $closing_count = $current_section = $last_key = 0;
  48. $sections = $sections_position = array();
  49.  
  50. foreach($list as $key => $tag) {
  51. if ($tag == 'o') {
  52. $openning_count++;
  53. if (!isset($sections_position[$current_section])) {
  54. $sections_position[$current_section] = array('start' => $key2position[$key], 'end' => '');
  55. }
  56. } elseif ($tag == 'c') {
  57. $closing_count++;
  58. if ($openning_count == $closing_count) {
  59. $sections_position[$current_section]['end'] = $key2position[$key];
  60. $openning_count = $closing_count = 0;
  61. for($i = $last_key; $i <= $key; $i++) {
  62. $sections[$i] = $current_section;
  63. }
  64. $last_key = $key + 1;
  65. $current_section++;
  66. }
  67. }
  68. }
  69. $this->set_list($positions_list, $key2position, $list, $sections, $sections_position);
  70. }
  71.  
  72. private function _analyse_list() {
  73. $text = $this->text;
  74. $tmp_text = '';
  75. $text_section = array();
  76.  
  77. list($openning_tag, $closing_tag) = $this->get_tags();
  78. $closing_tag_length = mb_strlen($closing_tag);
  79. list($positions_list, $key2position, $list, $sections, $sections_position) = $this->get_list();
  80.  
  81. $section_length = count($sections);
  82.  
  83. while ( ($closing_key = array_search('c', $list)) !== FALSE) {
  84. $openning_key = $closing_key - 1;
  85. $closing_position = $key2position[$closing_key];
  86. $openning_position = $key2position[$openning_key];
  87. $length = $closing_position - $openning_position;
  88. $inner_text = mb_substr($text, $openning_position, $length + $closing_tag_length);
  89. $text = mb_substr($text, 0, $openning_position) . mb_substr($text, $closing_position + $closing_tag_length);
  90.  
  91. $text_section[$sections[$closing_key]][] = $inner_text;
  92.  
  93. $list_length = count($key2position);
  94. for ($i = $closing_key + 1; $i <$list_length; $i++) {
  95. $key2position[$i]-=$length + $closing_tag_length;
  96. }
  97. unset($list[$openning_key]);
  98. unset($list[$closing_key]);
  99. unset($key2position[$closing_key]);
  100. unset($key2position[$openning_key]);
  101. unset($sections[$closing_key]);
  102. unset($sections[$openning_key]);
  103. $list = array_merge($list);
  104. $key2position = array_merge($key2position);
  105. $sections = array_merge($sections);
  106. }
  107. return $text_section;
  108. }
  109.  
  110. private function _generate_text($text_section) {
  111. list($positions_list, $key2position, $list, $sections, $sections_position) = $this->get_list();
  112. list($openning_tag, $closing_tag) = $this->get_tags();
  113. $closing_tag_length = mb_strlen($closing_tag);
  114. $text = $this->text;
  115. $sections_position_length = count($sections_position);
  116.  
  117. for ($i = 0; $i < $sections_position_length; $i++) {
  118. $replacement_string = '[section='.$i.']';
  119. $replacement_string_length = mb_strlen($replacement_string);
  120. $text = mb_substr($text, 0, $sections_position[$i]['start']).$replacement_string.mb_substr($text, $sections_position[$i]['end'] + $closing_tag_length);
  121.  
  122. $difference = $sections_position[$i]['end'] + $closing_tag_length - $sections_position[$i]['start'] - $replacement_string_length;
  123.  
  124. for ($j = $i + 1; $j < $sections_position_length; $j++) {
  125. $sections_position[$j]['start'] -= $difference;
  126. $sections_position[$j]['end'] -= $difference;
  127. }
  128.  
  129. }
  130.  
  131. $sections_length = count($text_section);
  132. for ($i = 0; $i < $sections_length; $i++) {
  133. $replacement_string = implode("", array_reverse($text_section[$i]));
  134.  
  135. $text = str_replace('[section='.$i.']', $replacement_string, $text);
  136. }
  137. return $text;
  138. }
  139.  
  140. private function _get_all_occurrences($haystack, $needle, $offset = 0) {
  141. $result = array();
  142. for($i = $offset; $i < mb_strlen($haystack); $i++) {
  143. if ( ($pos = mb_strpos($haystack, $needle, $i)) !== FALSE) {
  144. $offset = $pos;
  145. if ($offset >= $i) {
  146. $i = $offset;
  147. $result[] = $offset;
  148. }
  149. }
  150. }
  151. return $result;
  152. }
  153.  
  154. }


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