Alex Rivera | Logout

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

Asked 2008-08-05T00:35:17.493
59

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)
Edit
Report

1 Answer

8

From the preg_replace documentation on php.net:

replacement may contain references of the form \n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern.

See Flubba's example.

answered 2008-08-05T01:39:36.460

Your Answer