The Little Guy Posted January 25, 2007 Share Posted January 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/ Share on other sites More sharing options...
corbin Posted January 25, 2007 Share Posted January 25, 2007 So what exactly is $0 supposed to be? Quote Link to comment https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/#findComment-168685 Share on other sites More sharing options...
Hypnos Posted January 25, 2007 Share Posted January 25, 2007 If that's a var, remove the quotes. Quote Link to comment https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/#findComment-168693 Share on other sites More sharing options...
corbin Posted January 25, 2007 Share Posted January 25, 2007 It does work by the way. Preg_replace doesn't automatically alter variables (you may realize that, but in your post you don't use it that way), so it's necessary to echo it or $var = preg_replace with it. Quote Link to comment https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/#findComment-168700 Share on other sites More sharing options...
The Little Guy Posted January 25, 2007 Author Share Posted January 25, 2007 It doesn't workHere is the actual function:[code]<?phpfunction 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 Quote Link to comment https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/#findComment-168741 Share on other sites More sharing options...
The Little Guy Posted January 25, 2007 Author Share Posted January 25, 2007 the problem is that it doesn't know that $0 is supposed to match something from the first part, I don't know how to do the matching like in an .htaccess file. Anyone Help? Quote Link to comment https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/#findComment-169168 Share on other sites More sharing options...
TripleDES Posted January 5, 2009 Share Posted January 5, 2009 Bumping this thread. I have a similar scenario. See this string: $subject = "i am [[happy]] today"; I'd like to only change happy to uppercase. $pattern = "/\[\[.+?\]\]*/"; preg_replace($pattern, strtoupper(??whatdoidohere??), $val); Quote Link to comment https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/#findComment-730070 Share on other sites More sharing options...
RussellReal Posted January 5, 2009 Share Posted January 5, 2009 try \0 instead of $0 but I'm going to do a few tests right now and get back to you wait like 2 mins unless sum1 smarter than me gets to it first lol Quote Link to comment https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/#findComment-730094 Share on other sites More sharing options...
RussellReal Posted January 5, 2009 Share Posted January 5, 2009 <?php $a = "hello how ArE you?"; echo preg_replace("/\b(are)\b/ie","strtoupper('\\1')",$a); ?> you need the /e modifyer I guess it stands for "eval" lol Quote Link to comment https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/#findComment-730098 Share on other sites More sharing options...
TripleDES Posted January 5, 2009 Share Posted January 5, 2009 Still doesn't work: $myarray = "i am [[happy]] today"; $pattern = "/\[\[.+?\]\]*/ie"; foreach ($myarray as $val) { $val = preg_replace($pattern, "strtoupper('\\1')", $val); print $val . "\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/#findComment-730107 Share on other sites More sharing options...
TripleDES Posted January 5, 2009 Share Posted January 5, 2009 Ahh...I needed this: '\\0' Can you explain the significance of \\0, \\1, etc..?? Quote Link to comment https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/#findComment-730112 Share on other sites More sharing options...
RussellReal Posted January 6, 2009 Share Posted January 6, 2009 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 (?: ) Quote Link to comment https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/#findComment-730490 Share on other sites More sharing options...
DarkWater Posted January 6, 2009 Share Posted January 6, 2009 <?php $nedit = preg_replace('/(\s\'\w)/e', 'strtoupper("$1");', $nedit); Have fun. Quote Link to comment https://forums.phpfreaks.com/topic/35615-strtoupper-inside-a-preg_replace/#findComment-730491 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.