Foros del Web » Programando para Internet » PHP »

BBCode Regex: Lista desordenada

Estas en el tema de BBCode Regex: Lista desordenada en el foro de PHP en Foros del Web. Estoy necesitando una expresión regular que haga las veces de parser de bbcode, más específicamente para transformar listas como esta en html: Código: [lista] *opcion ...
  #1 (permalink)  
Antiguo 23/06/2006, 19:59
 
Fecha de Ingreso: febrero-2005
Mensajes: 670
Antigüedad: 19 años, 2 meses
Puntos: 0
BBCode Regex: Lista desordenada

Estoy necesitando una expresión regular que haga las veces de parser de bbcode, más específicamente para transformar listas como esta en html:

Código:
[lista]
*opcion
*opcion[/lista]
Código:
<ul>
<li>opcion</li>
<li>opcion</li>
</ul>
Estuve buscando un rato pero encontré soluciones bastantes complejas que involucraban varias funciones y que además servían para varios tipos de listas. Hay alguna forma de crear una expresión regular para resolver esta tarea?

Muchas gracias
Fede
__________________
Federico H. García
Desarrollo Web
www.federicog.com.ar
  #2 (permalink)  
Antiguo 30/06/2006, 12:28
Avatar de ASCENDEDMASTERS  
Fecha de Ingreso: julio-2005
Ubicación: Pergamino, Buenos Aires
Mensajes: 302
Antigüedad: 18 años, 9 meses
Puntos: 1
bajate el foro phpbb de phpbb.com y mira el codigo fuente del archivo function_posts.php y ahi estan las funciones que vos necesitas.
__________________
Dios es la unica fuente de todo bien.
  #3 (permalink)  
Antiguo 02/07/2006, 08:19
Avatar de ASCENDEDMASTERS  
Fecha de Ingreso: julio-2005
Ubicación: Pergamino, Buenos Aires
Mensajes: 302
Antigüedad: 18 años, 9 meses
Puntos: 1
creo que la funcion que andabas buscando son estas (analizalas bien porque copie y pegue tal cual las usan los foros phpbb)

Código:
function bbencode_second_pass($text, $uid)
{
	global $lang, $bbcode_tpl;

	$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);

	// pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0).
	// This is important; bbencode_quote(), bbencode_list(), and bbencode_code() all depend on it.
	$text = " " . $text;

	// First: If there isn't a "[" and a "]" in the message, don't bother.
	if (! (strpos($text, "[") && strpos($text, "]")) )
	{
		// Remove padding, return.
		$text = substr($text, 1);
		return $text;
	}

	// Only load the templates ONCE..
	if (!defined("BBCODE_TPL_READY"))
	{
		// load templates from file into array.
		$bbcode_tpl = load_bbcode_template();

		// prepare array for use in regexps.
		$bbcode_tpl = prepare_bbcode_template($bbcode_tpl);
	}

	// 
Código:
 and
for posting code (HTML, PHP, C etc etc) in your posts. $text = bbencode_second_pass_code($text, $uid, $bbcode_tpl); //
Cita:
and
for posting replies with quote, or just for quoting stuff. $text = str_replace("[quote:$uid]", $bbcode_tpl['quote_open'], $text); $text = str_replace("[/quote:$uid]", $bbcode_tpl['quote_close'], $text); // New one liner to deal with opening quotes with usernames... // replaces the two line version that I had here before.. $text = preg_replace("/\[quote:$uid=\"(.*?)\"\]/si", $bbcode_tpl['quote_username_open'], $text); //[list] and[list=x] for (un)ordered lists. // unordered lists $text = str_replace("[list:$uid]", $bbcode_tpl['ulist_open'], $text); // li tags $text = str_replace("[*:$uid]", $bbcode_tpl['listitem'], $text); // ending tags $text = str_replace("[/list:u:$uid]", $bbcode_tpl['ulist_close'], $text); $text = str_replace("[/list:o:$uid]", $bbcode_tpl['olist_close'], $text); // Ordered lists $text = preg_replace("/\[list=([a1]):$uid\]/si", $bbcode_tpl['olist_open'], $text); // colours $text = preg_replace("/\[color=(\#[0-9A-F]{6}|[a-z]+):$uid\]/si", $bbcode_tpl['color_open'], $text); $text = str_replace("[/color:$uid]", $bbcode_tpl['color_close'], $text); // size $text = preg_replace("/\[size=([1-2]?[0-9]):$uid\]/si", $bbcode_tpl['size_open'], $text); $text = str_replace("[/size:$uid]", $bbcode_tpl['size_close'], $text); // and for bolding text. $text = str_replace("[b:$uid]", $bbcode_tpl['b_open'], $text); $text = str_replace("[/b:$uid]", $bbcode_tpl['b_close'], $text); // and for underlining text. $text = str_replace("[u:$uid]", $bbcode_tpl['u_open'], $text); $text = str_replace("[/u:$uid]", $bbcode_tpl['u_close'], $text); // and for italicizing text. $text = str_replace("[i:$uid]", $bbcode_tpl['i_open'], $text); $text = str_replace("[/i:$uid]", $bbcode_tpl['i_close'], $text); // Patterns and replacements for URL and email tags.. $patterns = array(); $replacements = array(); // [img]image_url_here[/img] code.. // This one gets first-passed.. $patterns[] = "#\[img:$uid\]([^?].*?)\[/img:$uid\]#i"; $replacements[] = $bbcode_tpl['img']; // matches a xxxx://www.phpbb.com code.. $patterns[] = "#\[url\]([\w]+?://([\w\#$%&~/.\-;:=,?@\]+]|\[(?!url=))*?)\[/url\]#is"; $replacements[] = $bbcode_tpl['url1']; // www.phpbb.com code.. (no xxxx:// prefix). $patterns[] = "#\[url\]((www|ftp)\.([\w\#$%&~/.\-;:=,?@\]+]|\[(?!url=))*?)\[/url\]#is"; $replacements[] = $bbcode_tpl['url2']; // phpBB code.. $patterns[] = "#\[url=([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*?)\]([^?\n\r\t].*?)\[/url\]#is"; $replacements[] = $bbcode_tpl['url3']; // phpBB code.. (no xxxx:// prefix). $patterns[] = "#\[url=((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*?)\]([^?\n\r\t].*?)\[/url\]#is"; $replacements[] = $bbcode_tpl['url4']; // [email protected] code.. $patterns[] = "#\[email\]([a-z0-9&\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#si"; $replacements[] = $bbcode_tpl['email']; $text = preg_replace($patterns, $replacements, $text); // Remove our padding from the string.. $text = substr($text, 1); return $text; } // bbencode_second_pass() y esta otra, (aca atenes todos los regex tambien function bbencode_first_pass($text, $uid) { // pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0). // This is important; bbencode_quote(), bbencode_list(), and bbencode_code() all depend on it. $text = " " . $text; //
Código:
 and
for posting code (HTML, PHP, C etc etc) in your posts. $text = bbencode_first_pass_pda($text, $uid, '
Código:
', '
', '', true, ''); //
Cita:
and
for posting replies with quote, or just for quoting stuff. $text = bbencode_first_pass_pda($text, $uid, '
Cita:
', '
', '', false, ''); $text = bbencode_first_pass_pda($text, $uid, '/\
Cita:
Iniciado por \\\\&quot;(.*?)\\\\&quot;\
/is', '
', '', false, '', "[quote:$uid=\\\"\\1\\\"]"); //[list] and[list=x] for (un)ordered lists. $open_tag = array(); $open_tag[0] = "
  • "; // unordered.. $text = bbencode_first_pass_pda($text, $uid, $open_tag, "
", "[/list:u]", false, 'replace_listitems'); $open_tag[0] = "[list=1]"; $open_tag[1] = "
  1. "; // ordered. $text = bbencode_first_pass_pda($text, $uid, $open_tag, "
", "[/list:o]", false, 'replace_listitems'); // [color] and [/color] for setting text color $text = preg_replace("#\[color=(\#[0-9A-F]{6}|[a-z\-]+)\](.*?)\[/color\]#si", "[color=\\1:$uid]\\2[/color:$uid]", $text); // [size] and [/size] for setting text size $text = preg_replace("#\[size=([1-2]?[0-9])\](.*?)\[/size\]#si", "[size=\\1:$uid]\\2[/size:$uid]", $text); // and for bolding text. $text = preg_replace("#\[b\](.*?)\[/b\]#si", "[b:$uid]\\1[/b:$uid]", $text); // and for underlining text. $text = preg_replace("#\[u\](.*?)\[/u\]#si", "[u:$uid]\\1[/u:$uid]", $text); // and for italicizing text. $text = preg_replace("#\[i\](.*?)\[/i\]#si", "[i:$uid]\\1[/i:$uid]", $text); // [img]image_url_here[/img] code.. $text = preg_replace("#\[img\]((http|ftp|https|ftps)://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png)))\[/img\]#sie", "'[img:$uid]\\1' . str_replace(' ', '%20', '\\3') . '[/img:$uid]'", $text); // Remove our padding from the string.. return substr($text, 1);; } // bbencode_first_pass()
__________________
Dios es la unica fuente de todo bien.

Última edición por ASCENDEDMASTERS; 02/07/2006 a las 08:35
  #4 (permalink)  
Antiguo 02/07/2006, 08:29
Avatar de ASCENDEDMASTERS  
Fecha de Ingreso: julio-2005
Ubicación: Pergamino, Buenos Aires
Mensajes: 302
Antigüedad: 18 años, 9 meses
Puntos: 1
bueno, fijate que como tiene codigo bbcode, te lo interpreta en este foro, asique te combiene hacer click en asi citas lo que escribi y ves todo el codigo completo sin que interprete algunos bbcode este foro ;)

saludos y suerte.
__________________
Dios es la unica fuente de todo bien.

Última edición por ASCENDEDMASTERS; 02/07/2006 a las 08:37
  #5 (permalink)  
Antiguo 02/07/2006, 15:59
 
Fecha de Ingreso: febrero-2005
Mensajes: 670
Antigüedad: 19 años, 2 meses
Puntos: 0
Muchas gracias, voy a ver si puedo sacarlo de ahí.
__________________
Federico H. García
Desarrollo Web
www.federicog.com.ar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:00.