glorybloodyhell
13-04-2005, 01:45
lấy ví dụ tôi có function sau để chèn tag cho diễn đàn, làm sao để khi chèn tag code trên một đoạn có các tags khác thì nó không ứng dụng (ignore) các tags khác trong đoạn được chèn? thanks for help...
function upbcode($text) {
if(substr_count($text,"[") > 0 && substr_count($text,"]") > 0) {
$text = preg_replace("#\[size=([1-2]?[0-9])\](.*?)\[/size\]#si", "<font size=\\1>\\2</font>", $text);
$text = preg_replace("#\[font=(\#[0-9a-f]{6}|[a-z]+)\](.*?)\[/font\]#si","<font face=\\1>\\2</font>",$text);
$text = preg_replace("#\[color=(\#[0-9a-f]{6}|[a-z]+)\](.*?)\[/color\]#si",'<font color="\1">\2</font>',$text);
$text = preg_replace("#\[u\](.*?)\[/u\]#si",'<u>\1</u>',$text);
$text = preg_replace("=\[reala\]([^\[]*)\[/reala\]=si", '<embed src="$1" align="baseline" border="0" width="275" height="40" type="audio/x-pn-realaudio-plugin" console="Clip1" controls="ControlPanel,StatusBar" autostart="false"></embed>',$text);
$text = preg_replace("=\[realv\]([^\[]*)\[/realv\]=si", '<object name="rpcontrol" width=320 height=240 CLASSID="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"><param name="controls" value="ImageWindow,StatusBar"><param name="autostart" value="false"><param name="src" value="$1"></object><br><INPUT TYPE="button" VALUE="Play" onClick="document.rpcontrol.DoPlay()"><INPUT TYPE="button" VALUE="Pause" onClick="document.rpcontrol.DoPause()"><INPUT TYPE="button" VALUE="Stop" onClick="document.rpcontrol.DoStop()">',$text);
$text = preg_replace('=\[flash\=([^\[]*)\,([^\[]*)\]([^\[]*)\[/flash\]=si','<object classid="clsid: D27CDB6E-AE6D-11cf-96B8-444553540000" width="$1" height="$2"><param name=movie value="$3"><param name=play value=true><param name=loop value=true><param name=quality value=high><param name=backgroundcolor value=transparent><embed src="$3" width="$1" height="$2" play=true loop=true quality=high backgroundcolor=transparent></embed></object>',$text);
$text = preg_replace('=\[wmp\=([^\[]*)\,([^\[]*)\]([^\[]*)\[/wmp\]=si','<embed src="$3" width="$1" height="$2" autostart=0></embed>',$text);
$text = preg_replace("#\[s\](.*?)\[/s\]#si",'<s>\1</s>',$text);
$text = preg_replace("#\[i\](.*?)\[/i\]#si",'<i>\1</i>',$text);
$text = preg_replace("#\[b\](.*?)\[/b\]#si",'<b>\1</b>',$text);
$text = preg_replace("#\[center\](.*?)\[/center\]#si",'<center>\1</center>',$text);
$text = preg_replace("#\[marquee\](.*?)\[/marquee\]#si",'<marquee>\1</marquee>',$text);
$text = preg_replace("#\[url\]([a-z]+?://)([^\[]*)\[/url\]#si",'<a href="\1\2" target="_blank">\1\2</a>',$text);
$text = preg_replace("#\[url\]([^\[]*)\[/url\]#si",'<a href="http://\1" target="_blank">\1</a>',$text);
$text = preg_replace("#\[url=([a-z]+?://)([^\]]*)\](.*?)\[/url\]#si",'<a href="\1\2" target="_blank">\3</a>',$text);
$text = preg_replace("#\[url=([^\]]*)\](.*?)\[/url\]#si",'<a href="http://\1" target="_blank">\2</a>',$text);
$text = preg_replace("#\[email\]([^\[]+@[^\[]+)\[/email\]#si",'<a href="mailto:\1">\1</a>',$text);
$text = preg_replace('=\[img\](http:[^\[]*|[^\[:]*)\[/img\]=si','<img src="$1" border="0">',$text);
$text = preg_replace('=\[img\=([^\[]*)\,([^\[]*)\](http:[^\[]*|[^\[:]*)\[/img\]=si','<img src="$3" width="$1" height="$2" border="0">',$text);
do {
$text = preg_replace("#\[quote\](.*?)\[/quote\]#si",'<center><table border="0" cellspacing="0" cellpadding="0" width="95%"><tr><td><span class="small"><b>Quote:</b></span><table border="0" cellspacing="0" cellpadding="5" width="100%"><tr><td style="border: 1 black solid; color=blue" bgcolor=#E9ECF1><span class="quote">\1</span></td></tr></table></td></tr></table></center>',$text);
} while(preg_match("#\[quote\](.*?)\[/quote\]#si",$text));
do {
$text = preg_replace("#\[code\](.*?)\[/code\]#si",'<center><table border="0" cellspacing="0" cellpadding="0" width="95%"><tr><td><span class="small"><b>Code:</b></span><table border="0" cellspacing="0" cellpadding="5" width="100%"><tr><td style="border: 1 black solid; color=red" bgcolor=#E9ECF1><span class="code">\1</span></td></tr></table></td></tr></table></center>',$text);
} while(preg_match("#\[code\](.*?)\[/code\]#si",$text));
}
return $text;
}
function upbcode($text) {
if(substr_count($text,"[") > 0 && substr_count($text,"]") > 0) {
$text = preg_replace("#\[size=([1-2]?[0-9])\](.*?)\[/size\]#si", "<font size=\\1>\\2</font>", $text);
$text = preg_replace("#\[font=(\#[0-9a-f]{6}|[a-z]+)\](.*?)\[/font\]#si","<font face=\\1>\\2</font>",$text);
$text = preg_replace("#\[color=(\#[0-9a-f]{6}|[a-z]+)\](.*?)\[/color\]#si",'<font color="\1">\2</font>',$text);
$text = preg_replace("#\[u\](.*?)\[/u\]#si",'<u>\1</u>',$text);
$text = preg_replace("=\[reala\]([^\[]*)\[/reala\]=si", '<embed src="$1" align="baseline" border="0" width="275" height="40" type="audio/x-pn-realaudio-plugin" console="Clip1" controls="ControlPanel,StatusBar" autostart="false"></embed>',$text);
$text = preg_replace("=\[realv\]([^\[]*)\[/realv\]=si", '<object name="rpcontrol" width=320 height=240 CLASSID="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"><param name="controls" value="ImageWindow,StatusBar"><param name="autostart" value="false"><param name="src" value="$1"></object><br><INPUT TYPE="button" VALUE="Play" onClick="document.rpcontrol.DoPlay()"><INPUT TYPE="button" VALUE="Pause" onClick="document.rpcontrol.DoPause()"><INPUT TYPE="button" VALUE="Stop" onClick="document.rpcontrol.DoStop()">',$text);
$text = preg_replace('=\[flash\=([^\[]*)\,([^\[]*)\]([^\[]*)\[/flash\]=si','<object classid="clsid: D27CDB6E-AE6D-11cf-96B8-444553540000" width="$1" height="$2"><param name=movie value="$3"><param name=play value=true><param name=loop value=true><param name=quality value=high><param name=backgroundcolor value=transparent><embed src="$3" width="$1" height="$2" play=true loop=true quality=high backgroundcolor=transparent></embed></object>',$text);
$text = preg_replace('=\[wmp\=([^\[]*)\,([^\[]*)\]([^\[]*)\[/wmp\]=si','<embed src="$3" width="$1" height="$2" autostart=0></embed>',$text);
$text = preg_replace("#\[s\](.*?)\[/s\]#si",'<s>\1</s>',$text);
$text = preg_replace("#\[i\](.*?)\[/i\]#si",'<i>\1</i>',$text);
$text = preg_replace("#\[b\](.*?)\[/b\]#si",'<b>\1</b>',$text);
$text = preg_replace("#\[center\](.*?)\[/center\]#si",'<center>\1</center>',$text);
$text = preg_replace("#\[marquee\](.*?)\[/marquee\]#si",'<marquee>\1</marquee>',$text);
$text = preg_replace("#\[url\]([a-z]+?://)([^\[]*)\[/url\]#si",'<a href="\1\2" target="_blank">\1\2</a>',$text);
$text = preg_replace("#\[url\]([^\[]*)\[/url\]#si",'<a href="http://\1" target="_blank">\1</a>',$text);
$text = preg_replace("#\[url=([a-z]+?://)([^\]]*)\](.*?)\[/url\]#si",'<a href="\1\2" target="_blank">\3</a>',$text);
$text = preg_replace("#\[url=([^\]]*)\](.*?)\[/url\]#si",'<a href="http://\1" target="_blank">\2</a>',$text);
$text = preg_replace("#\[email\]([^\[]+@[^\[]+)\[/email\]#si",'<a href="mailto:\1">\1</a>',$text);
$text = preg_replace('=\[img\](http:[^\[]*|[^\[:]*)\[/img\]=si','<img src="$1" border="0">',$text);
$text = preg_replace('=\[img\=([^\[]*)\,([^\[]*)\](http:[^\[]*|[^\[:]*)\[/img\]=si','<img src="$3" width="$1" height="$2" border="0">',$text);
do {
$text = preg_replace("#\[quote\](.*?)\[/quote\]#si",'<center><table border="0" cellspacing="0" cellpadding="0" width="95%"><tr><td><span class="small"><b>Quote:</b></span><table border="0" cellspacing="0" cellpadding="5" width="100%"><tr><td style="border: 1 black solid; color=blue" bgcolor=#E9ECF1><span class="quote">\1</span></td></tr></table></td></tr></table></center>',$text);
} while(preg_match("#\[quote\](.*?)\[/quote\]#si",$text));
do {
$text = preg_replace("#\[code\](.*?)\[/code\]#si",'<center><table border="0" cellspacing="0" cellpadding="0" width="95%"><tr><td><span class="small"><b>Code:</b></span><table border="0" cellspacing="0" cellpadding="5" width="100%"><tr><td style="border: 1 black solid; color=red" bgcolor=#E9ECF1><span class="code">\1</span></td></tr></table></td></tr></table></center>',$text);
} while(preg_match("#\[code\](.*?)\[/code\]#si",$text));
}
return $text;
}