在PHP中使用preg_replace时如何找到匹配项? [英] How can I get at the matches when using preg_replace in PHP?

查看:184
本文介绍了在PHP中使用preg_replace时如何找到匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图抓住几个单词的大写字母,然后将它们包裹在span标签中.我正在使用 preg_replace 进行提取和包装,但没有输出任何内容

I am trying to grab the capital letters of a couple of words and wrap them in span tags. I am using preg_replace for extract and wrapping purposes, but it's not outputting anything.

preg_replace("/[A-Z]/", "<span class=\"initial\">$1</span>", $str)

推荐答案

您需要将模式放在括号/([A-Z])/中,如下所示:

You need to put the pattern in parentheses /([A-Z])/, like this:

preg_replace("/([A-Z])/", "<span class=\"initial\">$1</span>", $str)

这篇关于在PHP中使用preg_replace时如何找到匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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