Manipulating preg_replaces's 'replace' value
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", "[na开发者_C百科me=" . strtoupper($1) . "]" ,$str )
and
preg_replace("#\[name=((?:keywords)|(?:description))\]#is", "[name={strtoupper($1)}]" ,$str )
but none work.
any help is muchly appreciated.
You can use the e
modifier as:
preg_replace("#\[name=((?:keywords)|(?:description))\]#ise", "'[name='.strtoupper('\\1'). ']'" ,$str )
Code In Action
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
)
精彩评论