Jump to content

Text replacing


scvinodkumar

Recommended Posts

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... :)

 

 

Link to comment
https://forums.phpfreaks.com/topic/184242-text-replacing/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/184242-text-replacing/#findComment-972732
Share on other sites

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/184242-text-replacing/#findComment-972740
Share on other sites

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]

Link to comment
https://forums.phpfreaks.com/topic/184242-text-replacing/#findComment-973284
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/184242-text-replacing/#findComment-973305
Share on other sites

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.  :sarcastic:

 

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);

Link to comment
https://forums.phpfreaks.com/topic/184242-text-replacing/#findComment-973538
Share on other sites

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.