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]低头思故乡[/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当中的值变为$replace的效果
$ubb=preg_replace($pattern,$replace,$string);
echo $ubb;
?>

The output result is shown on the right


Continuing Learning
||
<?php $string='[b]静夜思[/b] [color=Red]李白[/color] [i]床前明月光[/i] [u]疑是地上霜[/u] [i]举头望明月[/i] [u]低头思故乡[/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当中的值变为$replace的效果 $ubb=preg_replace($pattern,$replace,$string); echo $ubb; ?>
submitReset Code