Convert uppercase matches to bold using regex

2020-04-20 04:03发布

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']);

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

2条回答
趁早两清
2楼-- · 2020-04-20 04:27

You need to capture the group and enclose the pattern:

preg_replace("/\b([A-Z]{2,})\b/", "<b>\\1</b>", $_POST['descricao']);
查看更多
聊天终结者
3楼-- · 2020-04-20 04:41

Use this:

$_POST['descricao'] = "UPPERCASE test WORD"
$_POST['descricao'] = preg_replace("/\b([A-Z]{2,})\b/", "<b>$1</b>", $_POST['descricao']);
查看更多
登录 后发表回答