Jump to content

strtoupper() inside a preg_replace()


The Little Guy

Recommended Posts

I would like to take this and uppercase the $0
[b]$nedit = "Don't 'n sfsdf";
preg_replace('/(\s\'\w)/', '$0', $nedit);[/b]


if I do this:
[b]preg_replace('/(\s\'\w)/', strtoupper('$0'), $nedit);[/b]

nothing happens  I know it is matching, because we take $0 and make it some random thing, and it finds it.
Link to comment
https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/
Share on other sites

It doesn't work


Here is the actual function:
[code]
<?php
function edit_name($nedit){
#$nedit = strtolower($nedit);
$nedit = str_replace(array("/", "&", "  "), array("", "and", " "), $nedit);
$nedit = preg_replace('/^(a|an|the)\s+(.+)/i', '$2, $1', $nedit);
# $nedit = preg_replace('/(?<![a-z]\')\b[a-z]/e', "strtoupper('$0')", $nedit);
#$nedit = preg_replace('/^([^\']*)\B(\'\w)(.*)$/', "$0" . strtoupper('$1') . "$2", $nedit);
$nedit = preg_replace('/(\s\'\w)/', '$0', $nedit);
return $nedit;
//return /*addslashes(*/$nedit /*)*/;
}


echo edit_name("Don't 'n sfsdf");
echo $repls;

?>
[/code]

And here are the results: http://viplyrics.com/testing.php
  • 1 year later...

they are basck references

 

\0 will be the whole string captured inside of the pattern..

 

\1 would be the first capturing group

\2 wouldbe the second

\3 would be the third.. etc

 

capturing groups are ( )

 

and non-capturing groups are (?: )

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.