
Originariamente inviata da
ciro78
ma sarebbe più semplice ripulire la stringa dei tag list e [*] sostituendoli con "" e poi usare explode ?
no impossibile, poi come farei in caso di <list> annidate, tag [*] sparsi in giro per il codice o bbcode mal inserito?
comunque ho risolto in questo modo, posto il codice nel caso fosse d'aiuto a qualcuno:
Codice PHP:
<?php
function parseCode2($text) {
// regexp
$regexp = "/" .
"(\
[list\])" .
"|" .
"(\[\*\].*?\[\/\*\])" .
"|" .
"(\[\/list\])" .
"|" .
"(\[code\])" .
"|" .
"(\[\/code\])" .
"|" .
"(\[quote(\s*=\s*(.*?){1}){0,1}\s*\])" .
"|" .
"(\[\/quote\])" .
"/";
$code = preg_split ( $regexp, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
//
$text = "";
$open_list = $closed_list = $item_list = $open_code = $closed_code = $open_quote = $closed_quote = 0;
//
while ($html = array_shift( $code )) {
// <ul> tag
if (preg_match("/\
[list\]/", $html)) {
$open_list++;
$text .= "<ul>";
}
// <li> tag
elseif (preg_match("/\[\*\](.*?)\[\/\*\]/", $html, $return)) {
if ($open_list) {
$text .= "<li>{$return[1]}</li>";
}
}
// </ul> tag
elseif (preg_match("/\[\/list\]/", $html)) {
$open_list--;
$text .= "</ul>";
}
// all
else {
$text .= $html;
}
}
// <b> tag
$text = preg_replace("/\[b\](.*?)\[\/b\]/", "<b>\\1</b>", $text);
// <u> tag
$text = preg_replace("/\[u\](.*?)\[\/u\]/", "<u>\\1</u>", $text);
// <i> tag
$text = preg_replace("/\[i\](.*?)\[\/i\]/", "<i>\\1</i>", $text);
// <img src=http://example.jpg> tag
$text = preg_replace("/\[img\](http|https|ftp){1}(:\/\/){1}(.*?)\[\/img\]/", "<img src=\"\\1\\2\\3\" />", $text);
// <span style='font-size:[value]pt;line-height:100%'>text</span> tag
$text = preg_replace("#\[size=([1-7]+)\](.+?)\[/size\]#is", "<span style='font-size:\\1pt;line-height:100%'>\\2</span>", $text);
// <span style='color:color;line-height:100%'>text</span> tag
$text = preg_replace("#\[color=(red|blue|purple|orange|yellow|gray|green)\](.+?)\[/color\]#is", "<span style='color:\\1;line-height:100%'>\\2</span>", $text);
// <span style='font-family:font>text</span> tag
$text = preg_replace("#\[font=(arial|times|courier|impact|geneva|optima)\](.*?)\[/font\]#is", "<span style='font-family:\\1'>\\2</span>", $text);
// <a href=mailto:example@domain.dot>click here</a> tag
$text = preg_replace( "/\[email\](.*?)\[\/email\]/i", "<a href='mailto:\\1'>\\1</a>", $text);
$text = preg_replace( "#\[email\s*=\s*([\.\w\-]+\@[\.\w\-]+\.[\w\-]+)\s*\](.*?)\[\/email\]#is", "<a href='mailto:\\1'>\\2</a>", $text);
// <a href=http://www.google.it>click here</a> tag
$text = preg_replace("/\[url\](http|https|ftp){1}(:\/\/){1}(.*?)\[\/url\]/is", "<a href=\"\\1\\2\\3\">\\1\\2\\3</a>", $text);
$text = preg_replace( "#\[url\s*=\s*(http|https|ftp){1}(:\/\/){1}(.*?)\s*\](.*?)\[\/url\]#is", "<a href=\"\\1\\2\\3\">\\4</a>", $text);
// <a href=http://www.google.it>click here</a> tag
echo $text;
}
$text = "
[b]b[/b]
[i]i[/i]
[u]u[/u]
[size=7]dfsdfsfds[/size]
[url=http://google.it]http://javascript:alert(document.cookie)[/url]
[url]httdp://google.it[/url]
[img]http://google.it/image.gif[/img]
[email]admin@admin.it[/email]
[size=7][color=red]LAL[/color][/size]
[color=red]LAL[/color]
[font=impact]asd[/font]
[list]
[*]asd[/*]
[*]lol[/*]
[/list]
";
echo parseCode2($text);
?>