使用正则表达式将大写匹配转换为粗体 [英] Convert uppercase matches to bold using regex

查看:62
本文介绍了使用正则表达式将大写匹配转换为粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一个字符串中的所有大写单词并将其设置为粗体

i need to find all uppercase words in a string and set it bold

$_POST['descricao'] = "UPPERCASE test WORD"
$_POST['descricao'] = preg_replace("\b[A-Z]{2,}\b", "<b>\\1</b>", $_POST['descricao']);

它应该返回:UPPERCASE;测试<b>WORD</b>

it should return: <b>UPPERCASE</b> test <b>WORD</b>

推荐答案

需要捕获组并附上模式:

You need to capture the group and enclose the pattern:

preg_replace("/\b([A-Z]{2,})\b/", "<b>\\1</b>", $_POST['descricao']);

这篇关于使用正则表达式将大写匹配转换为粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆