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 = \'\';
$text_section = array();
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]);
$list = array_merge($list);
$key2position = array_merge($key2position);
$sections = array_merge($sections);
}
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++) {
$replacement_string = implode(\"\", array_reverse($text_section[$i]));
$text = str_replace(\'[section=\'.$i.\']\', $replacement_string, $text);
}
return $text;
}
private function _get_all_occurrences($haystack, $needle, $offset = 0) {
$result = array();
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;
}
}
Demo skryptu:
https://brokers-star.com/bbcode/Potrzebuję poprawki, która zmieni kolejność wyświetlania rozdzielonych cytatów.
Czyli mając początkowe cytaty w takiej formie:
Cytat(andrew)
Cytat(tomek)
Cytat(olek11)
treść komentarza olek
komentarz tomek
koment dla andrew
ozs
Cytat(tomasz)
dasdsadadad
A po poprawieniu skryptu, chciałbym aby zmieniał na taką:
Cytat
Cytat(olek11)
treść komentarza olek
Cytat(tomek)
komentarz tomek
Cytat(andrew)
koment dla andrew
ozs
Cytat(tomasz)
dasdsadadad
Chętnych z ofertą cenową proszę o kontakt na gg: 2476895
Ten post edytował xmen491 16.04.2013, 22:53:39