views:

25

answers:

2

I'm trying to alter the match value from preg_replace with an uppcase copy but cant seem to figure it out...

I've tried:

preg_replace("#\[name=((?:keywords)|(?:description))\]#is", "[name=" . strtoupper($1) . "]" ,$str )

and

preg_replace("#\[name=((?:keywords)|(?:description))\]#is", "[name={strtoupper($1)}]" ,$str )

but none work.

any help is muchly appreciated.

+1  A: 

You can use a callback function and preg_replace_callback for this.

E.g. (untested):

preg_replace(
    "#\[name=((?:keywords)|(?:description))\]#is",
    create_function('$matches', 'return "[name=" . strtoupper($matches[1]) . "]"'),
    $str
)
deceze
+2  A: 

You can use the e modifier as:

preg_replace("#\[name=((?:keywords)|(?:description))\]#ise", "'[name='.strtoupper('\\1'). ']'" ,$str )

Code In Action

codaddict