Strona 1 z 1

Parsowanie BBCode z zapytania SQL do bazy danych

: 06 maja 2006, 20:59
autor: athlan
Kiedyś zakładałem topic o parsowaniu tekstu (np topica z bazy danych phpBB): http://www.przemo.org/phpBB2/forum/view ... hp?t=25729 (skoro już zamknięty i nie mogę nic dodać to piszę nowy :) )

Mam jednak problem... zrobiłen takie oto zapytanie:

Kod: Zaznacz cały

$sql = "SELECT topic_id,topic_title,topic_time,topic_poster FROM ".$pack_forum_prefix."topics WHERE forum_id = '$pack_forum_category' ORDER BY topic_time ASC LIMIT 0,$pack_set_text_limit";
zaznacza on ostatnie topicki z danej kategorii

2 zapytanie wewnątrz pętli white tego powyżej:

Kod: Zaznacz cały

$sql2 = "SELECT username FROM ".$pack_forum_prefix."users WHERE user_id = '".$array['topic_poster']."'";
zaznacza ono usera, który postował

i jeszcze jedno wewnątrz pęli pierwszego:

Kod: Zaznacz cały

$sql3 = "SELECT bbcode_uid,post_text FROM ".$pack_forum_prefix."posts_text WHERE post_subject = '".$array['topic_title']."'";
zaznacza tekst do topicku, dodatkowo robię takie coś, aby po części pozbyć się tego kodowania w bbcode:

Kod: Zaznacz cały

$post_text = str_replace(":$bbcode_coding", '', $post_text);
zamieniam te kodowanie na nic :)

I teraz mam taki wynik działania mojego skryptu (chodzi o sam tekst topicku):

Kod: Zaznacz cały

[URL=http://przemo.org]test[/URL] [b]test[/b] [size=18][shadow=red]test[/shadow][/size] t e s t . . . . . . . n e w s a
dołączam screen w załączniku:
Obrazek

chciałbym przeparsować bbcde ale nie zabardzo wiem jak... mam napisac swój parser, czy wykorzystać ten z phpBB?
Jeżeli ten z phpBB to gdzie on jest?

(oprócz funkcji w pliku includes/bbcode.php, bo dla mnie tam są zbędne funkcje dla mojego skryptu, które m.in. usówają/dodają to kodowanie)

P.S. Jak patrzyłem w viewtopic.php i poprawnie mi chciał przeparsować tekst to wyplówał że nie może dołączyć common.php (bo ścieżka mojego skryptu względem forum jest zupełnie inna, forum: /forum/ , a skrypt: /modules/scripts/script.php, czyli względem forum: ../../forum/. Dlatego, że plik common chce być wywołany z: /modules/scripts/common.php, ale tam go nie ma bo po co :) )

P.S. 2: Zauważyłem coś: /forum/templates/subSilver/bbcode.tpl :), to chyba rozwiązanie mojego problemu, ale jak tego użyć?

: 07 maja 2006, 08:05
autor: quba
chcesz w ogóle usunąć znaki bbcode?
W funkcji jako argument podajesz treść wyciągniętą z bazy i bbcode_uid danego posta:

Kod: Zaznacz cały

function bbencode_strip($text, $uid)
{
	$text = ' ' . $text;

	if ( !(strpos($text, "[") && strpos($text, "]")) )
	{
		$text = substr($text, 1);
		return $text;
	}

	$text = str_replace("[code:1:$uid]","", $text);
	$text = str_replace("[/code:1:$uid]", " ", $text);
	$text = str_replace("[code:$uid]", "", $text);
	$text = str_replace("[/code:$uid]", " ", $text);

	$text = str_replace("[quote:1:$uid]","", $text);
	$text = str_replace("[/quote:1:$uid]", " ", $text);
	$text = str_replace("[quote:$uid]", "", $text);
	$text = str_replace("[/quote:$uid]", " ", $text);

	$text = preg_replace("/\[quote:$uid=(?:\"?([^\"]*)\"?)\]/si", "", $text);
	$text = preg_replace("/\[quote:1:$uid=(?:\"?([^\"]*)\"?)\]/si", "", $text);
	
	$text = str_replace("[list:$uid]", "", $text);
	$text = str_replace("[*:$uid]", " ", $text);
	$text = str_replace("[/list:u:$uid]", " ", $text);
	$text = str_replace("[/list:o:$uid]", " ", $text);
	$text = preg_replace("/\[list=([a1]):$uid\]/si", "", $text);

	$text = preg_replace("/\[color=(\#[0-9A-F]{6}|[a-z]+):$uid\]/si", "", $text);
	$text = str_replace("[/color:$uid]", " ", $text);

	$text = str_replace("[url]","", $text);
	$text = str_replace("[/url]", " ", $text);
	$text = str_replace("[/URL]", " ", $text);

	$text = preg_replace("/\[url=([a-z0-9\-\.,\?!%\*_\/:;~\\&$@\/=\+]+)\]/si", "", $text);
	$text = str_replace("[/url]", " ", $text);

	$text = str_replace("[img:$uid]","", $text);
	$text = str_replace("[/img:$uid]", " ", $text);

	$text = str_replace("[email:$uid]","", $text);
	$text = str_replace("[/email:$uid]", " ", $text);

	$text = preg_replace("/\[size=([\-\+]?[1-2]?[0-9]):$uid\]/si", "", $text);
	$text = str_replace("[/size:$uid]", " ", $text);
	
	$text = preg_replace("/\[align=(left|right|center|justify):$uid\]/si", "", $text);
	$text = str_replace("[/align:$uid]", " ", $text);

	$text = str_replace("[b:$uid]","", $text);
	$text = str_replace("[/b:$uid]", " ", $text);

	$text = str_replace("[u:$uid]", "", $text);
	$text = str_replace("[/u:$uid]", " ", $text);

	$text = str_replace("[i:$uid]", "", $text);
	$text = str_replace("[/i:$uid]", " ", $text);

	$text = str_replace("[fade:$uid]", "", $text);
	$text = str_replace("[/fade:$uid]", " ", $text);

	$text = str_replace("[scroll:$uid]", "", $text);
	$text = str_replace("[/scroll:$uid]", " ", $text);

	$text = str_replace("[center:$uid]", "", $text);
	$text = str_replace("[/center:$uid]", " ", $text);

	$text = preg_replace("/\[glow=(\#[0-9A-F]{6}|[a-z]+):$uid\]/si", "", $text);
	$text = str_replace("[/glow:$uid]", " ", $text);

	$text = preg_replace("/\[shadow=(\#[0-9A-F]{6}|[a-z]+):$uid\]/si", "", $text);
	$text = str_replace("[/shadow:$uid]", " ", $text);

	$text = str_replace("[g:$uid]", "", $text);
	$text = str_replace("[/g:$uid]", " ", $text);

	$text = str_replace("[you:$uid]", "you", $text);

	$text = preg_replace("#\[hide:$uid\](.*?)\[/hide:$uid\]#si"," ", $text);

	$text = preg_replace("#\[mod\](.*?)\[/mod\]#si"," ", $text);

	$text = substr($text, 1);

	return $text;
}