Một số hàm thường dùng: Viết hoa các ký tự đầu dòng PHP: $str = preg_replace_callback('/([\n])([\-|\"|\']?)([ ]?)([\"]?)([ ]?)([a-z]|đ|à|á|ả|ã|ạ|ă|ằ|ắ|ẳ|ẵ|ặ|â|ầ|ấ|ẩ|ẫ|ậ|è|é|ẻ|ẽ|ẹ|ê|ề|ế|ể|ễ|ệ|ì|í|ỉ|ĩ|ị|ò|ó|ỏ|õ|ọ|ô|ồ|ố|ổ|ỗ|ộ|ơ|ờ|ớ|ở|ỡ|ợ|ù|ú|ủ|ũ|ụ|ư|ừ|ứ|ử|ữ|ự|ỳ|ý|ỷ|ỹ|ỵ){1}/', function ($matches) {return mb_strtoupper($matches[0],'UTF-8');},$str ); PHP: // Viết hoa ký tự đầu dòng, lồng nhiều BBCode vẫn dùng được $str = implode("\n", array_map(function($line) { return preg_replace_callback( '/^((?:\[[^\]]+\]\s*)*)([^a-zA-ZÀ-ỹđĐ]*)(\p{Ll})/u', fn($m) => $m[1] . $m[2] . mb_strtoupper($m[3], 'UTF-8'), $line ); }, preg_split('/\R/u', $str))); Viết hoa sau dấu câu: !:; PHP: $str = preg_replace_callback('/([^.])([\!|.|?|:])([ ]+)([\"]?)([a-z]|đ|à|á|ả|ã|ạ|ă|ằ|ắ|ẳ|ẵ|ặ|â|ầ|ấ|ẩ|ẫ|ậ|è|é|ẻ|ẽ|ẹ|ê|ề|ế|ể|ễ|ệ|ì|í|ỉ|ĩ|ị|ò|ó|ỏ|õ|ọ|ô|ồ|ố|ổ|ỗ|ộ|ơ|ờ|ớ|ở|ỡ|ợ|ù|ú|ủ|ũ|ụ|ư|ừ|ứ|ử|ữ|ự|ỳ|ý|ỷ|ỹ|ỵ){1}/', function ($matches) {return $matches[1].mb_strtoupper(mb_substr($matches[0],1),'UTF-8');} ,$str ); Viết hoa ký tự đầu dòng, có 1 thẻ BBCode vẫn được PHP: $str = preg_replace_callback( '/(^|\R)((?:\[[^\]]+\]\s*)*)([^\p{L}]*)?(\p{Ll})/ui', function ($m) { return $m[1] . ($m[2] ?? ''). ($m[3] ?? ''). mb_strtoupper($m[3], 'UTF-8'); }, $str ); Fix thẻ BBCode IMG PHP: $str = preg_replace_callback('/\[IMG]([^\]]+)\[\/IMG]/i',function ($matches) {return str_replace(' ','',$matches[0]);} ,$str);$str = preg_replace_callback('/\[IMG size=([^\]]+)]([^\]]+)\[\/IMG]/i',function ($matches) {return str_replace(' ','',$matches[0]);} ,$str); Tự động nhận diện đoạn hội thoại - trong dấu ngoặc: "Đoạn hội thoại" PHP: $str = preg_replace('/([:|,|.])([ ])([\"])([^\n|\"]+)([\"])([\.]?)/', ":\n$3$4$5", $str); # Tách đoạn hội thoại xuống dòng$str = preg_replace('/([\n])([\"])([^\"]+[^.])([ ]?)([\"])/', '$1"$3."', $str); # Thêm dấu chấm vào cuối đoạn hội thoại$str = preg_replace('/([\n])([\"])([^\"]+)([\"])([^\n|\r]+)/', "$1$2$3$4\n$5", $str); # Tách xuống dòng đợt 2 Convert sang chữ thường nếu có chữ viết hoa trong dòng PHP: $content = preg_replace_callback('/\b[\p{Lu}]+\b/u', function ($m) { return mb_strtolower($m[0], 'UTF-8'); }, $content); Convert nếu nguyên 1 dòng toàn chữ VIẾT HOA PHP: $str = preg_replace_callback( '/^(?=[\P{Ll}]*\p{Lu})[\p{Lu}\P{L}]+$/mu', function ($m) { return mb_strtolower($m[0], 'UTF-8'); }, $str ); Convert sang chữ thường thẻ HEADING nếu nguyên 1 dòng toàn là chữ VIẾT HOA PHP: $str = preg_replace_callback( '/\[HEADING=(\d+)\](.*?)\[\/HEADING\]/isu', function ($matches) { $level = $matches[1]; $content = $matches[2]; $content = preg_replace_callback( '/^(?=[\P{Ll}]*\p{Lu})[\p{Lu}\P{L}]+$/mu', function ($m) { return mb_strtolower($m[0], 'UTF-8'); }, $content ); return "[HEADING={$level}]{$content}[/HEADING]"; }, $str );