Convert uppercase matches to bold using regex

时间秒杀一切 提交于 2019-12-02 13:01:15

问题


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>


回答1:


You need to capture the group and enclose the pattern:

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



回答2:


Use this:

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


来源:https://stackoverflow.com/questions/17866916/convert-uppercase-matches-to-bold-using-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!