scvinodkumar Posted December 7, 2009 Share Posted December 7, 2009 Hi i have the following text, Groups you own Your groups Create a new group I want to replace the word 'Groups,groups and group' with 'Organizations,organizations and organization' respectively from the above text. For example, The above text should like Organizations you own Your organizations Create a new organization Could you please help me on this... Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 7, 2009 Share Posted December 7, 2009 Hi i have the following text, Groups you own Your groups Create a new group I want to replace the word 'Groups,groups and group' with 'Organizations,organizations and organization' respectively from the above text. For example, The above text should like Organizations you own Your organizations Create a new organization Could you please help me on this... You can use str_replace, A really simple function and can simply replace 'groups' with 'organizations', and 'group' with 'organization', retaining the plural syntax. $phrase = "Organizations you own, Your organizations, Create a new organization"; $old = array("group", "groups"); $new = array("organization", "organizations"); $newphrase = str_replace($old, $new, $phrase); echo $newphrase; //Should be what you want it. Quote Link to comment Share on other sites More sharing options...
scvinodkumar Posted December 7, 2009 Author Share Posted December 7, 2009 Thanks for your help. Its working.... Quote Link to comment Share on other sites More sharing options...
thebadbad Posted December 7, 2009 Share Posted December 7, 2009 A way to take care of the casing: <?php $str = 'Groups you own Your groups Create a new group'; function _callback($matches) { $matches[0] = 'organization'; if (ctype_upper($matches[1])) { $matches[0] = ucwords($matches[0]); } return ($matches[2] ? $matches[0] . 's' : $matches[0]); } $str = preg_replace_callback('~\b(g)roup(s?)\b~i', '_callback', $str); echo $str; ?> Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted December 8, 2009 Share Posted December 8, 2009 Slick solution, thebadbad Got me thinking of an alternative one without the need of captures: $str = 'Groups you own Your groups Create a new group'; function _callback($matches){ $replace = (ctype_upper($matches[0][0]))? 'Organization': 'organization'; return (substr($matches[0], -1, 1) == 's')? $replace . 's': $replace; } echo $str = preg_replace_callback('#\bgroups?\b#i', '_callback', $str); [ot] Granted, this feels like a violation of DRY by replicating the word 'organization' twice in essence (could also be troubling in the event of a typo with one of the conditions). An alternative callback could then be: function _callback($matches){ $replace = 'organization'; if(ctype_upper($matches[0][0])){ $replace = ucfirst($replace); } return (substr($matches[0], -1, 1) == 's')? $replace . 's': $replace; } As usual, so many ways to solve a problem. At first glance, I was confused by your regex pattern (the captures threw me off initially).. wasn't until I examined your callback that I understood [/ot] Quote Link to comment Share on other sites More sharing options...
salathe Posted December 8, 2009 Share Posted December 8, 2009 I think this would be a really nice candidate for a closure in PHP 5.3, something along the lines of : function create_callback($replace) { return function ($match) use ($replace) { if (ctype_upper($match[0][0])) { $replace[0] = strtoupper($replace[0]); } return $replace; }; } echo $str = preg_replace_callback('/\bgroup(?=s?\b)/i', create_callback('organization'), $str); I also changed the regular expression so that the callback doesn't need to take care of pluralising. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted December 8, 2009 Share Posted December 8, 2009 Yet another slick solution ( good one salathe ) I particularly like the lookahead assertion dealing with the [optional s] and word boundery - as you said, doesn't need to take care of pluralsim. Bonus points for you. With all these solutions flying around, you'd think the holiday giving season is here or something. We could even base your solution on PHP 5.2.x: function _callback($matches, $replace = 'organization'){ if(ctype_upper($matches[0][0])){ $replace[0] = strtoupper($replace[0]); } return $replace; } echo $str = preg_replace_callback('/\bgroup(?=s?\b)/i', '_callback', $str); Quote Link to comment Share on other sites More sharing options...
scvinodkumar Posted December 11, 2009 Author Share Posted December 11, 2009 Thanks friends, its works well. Thanks for all your effort Quote Link to comment 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.