Basic PHP development tutorial: Simple UBB text editor
If you don’t understand this section, you can skip it. If you are interested, you can read it carefully in the future
Let’s take a look at the UBB editor. This is a text processing technology often used on websites. Because of the use of the UBB file editor, the format we specified can exist. If the format is not specified, users cannot display it on the website.
Let’s take a look at the effect:
<?php $string='[b]靜夜思[/b] [color=Red]李白[/color] [i]床前明月光[/i] [u]疑是地上霜[/u] [i]舉頭望明月[/i] [u]低頭思故鄉(xiāng)[/u] '; //匹配UBB字符 $pattern=array( '/\[b\](.*)\[\/b\]/i', '/\[i\](.*)\[\/i\]/iU', '/\[u\](.*?)\[\/u\]/i', '/\[color=(.*?)\](.*?)\[\/color\]/', ); //需要替換的UBB字符 $replace=array( '<b>\1</b><br />', '<i>\1</i><br />', '<u>\1</u><br />', '<font color="\1">\2</font><br />', ); //使用正則匹配$string,將$string當(dāng)中的值變?yōu)?replace的效果 $ubb=preg_replace($pattern,$replace,$string); echo $ubb; ?>
The output result is shown on the right