How can I get at the matches when using preg_replace in PHP? *

Question

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)

Answer

You need to put the match in parentheses, like this:

preg_replace("/([A-Z])/", "<span class=\"initial\">$1</span>", $str)
< br > via < a class="StackLink" href=" http://stackoverflow.com/questions/1746/" >How can I get at the matches when using preg_replace in PHP?< /a>
Share on Google Plus

About Cinema Guy

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment