Witam, posiadam kod, który rozdziela mi cytaty, wstawiane na moją stronę:
<?php
class Quote_Parser {
private $text;
public function __construct($text) {
$this->text = $text;
}
public function set_tags($openning, $closing) {
$this->tags = array($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) {
$this->list = array($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')); ksort($positions_list); // lista zawiera pozycje znacznikoww tekscie $key2position = array_keys($positions_list); // lista zawiera powiazanie pomiedzy kluczem glownym, a pozycyjnym $list = array_merge($positions_list); // zawiera liste z kolejnoscia poszczegolnych znacznikow $openning_count = $closing_count = $current_section = $last_key = 0;
$sections = $sections_position = array();
foreach($list as $key => $tag) {
if ($tag == 'o') {
$openning_count++;
if (!isset($sections_position[$current_section])) { $sections_position[$current_section] = array('start' => $key2position[$key], 'end' => ''); }
} 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();
$section_length = count($sections);
while ( ($closing_key = array_search('c', $list)) !== FALSE) { $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;
$list_length = count($key2position); for ($i = $closing_key + 1; $i <$list_length; $i++) {
$key2position[$i]-=$length + $closing_tag_length;
}
unset($list[$openning_key]); unset($list[$closing_key]); unset($key2position[$closing_key]); unset($key2position[$openning_key]); unset($sections[$closing_key]); unset($sections[$openning_key]); }
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;
$sections_position_length = count($sections_position);
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;
}
}
$sections_length = count($text_section); for ($i = 0; $i < $sections_length; $i++) {
$text = str_replace('[section='.$i.']', $replacement_string, $text); }
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-linkuTrzeba 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
Ten post edytował xmen491 14.04.2013, 17:31:07