我需要在字符串中查找所有大写单词并将其设置为粗体
$_POST['descricao'] = "UPPERCASE test WORD"
$_POST['descricao'] = preg_replace("\b[A-Z]{2,}\b", "<b>\\1</b>", $_POST['descricao']);
它应返回:< b>大写< / b>.测试< b> WORD< / b>
解决方法:
您需要捕获组并包含模式:
preg_replace("/\b([A-Z]{2,})\b/", "<b>\\1</b>", $_POST['descricao']);