How to change the end of a name

3

Changing the end of a name for example

WELITON, MAICON, WELIGTON

for

WELITOM, MAICOM, WELIGTOM

    
asked by anonymous 11.08.2016 / 00:13

5 answers

1

You can use preg_replace, see an example:

I created a function to return the changed name:

function alteraFinal($nome){
    $pattern = ['/ON\b/'];
    $replace = ['OM'];
    return preg_replace($pattern,$replace, $nome);
}

Or if you prefer, a reduced code version:

function alteraFinal($nome){
    return preg_replace('/ON\b/i','OM', $nome);
}

Usage:

echo alteraFinal('WELITON');
echo '<br/>';
echo alteraFinal('MAICON');
echo '<br/>';
echo alteraFinal('WELIGTON');
echo '<br/>';
echo alteraFinal('CONRRADO');

Result:

  

WELITOM

     

MAICOM

     

WELIGTOM

     

CONRRADO

I hope I have helped!

    
11.08.2016 / 02:55
2

After you create the default regex, use the preg_replace function to do the override. The default should be to find the characters you want to replace, the new characters, and the text that will change. link

    
11.08.2016 / 00:45
1

I got it so

    $nomeLimpo = preg_replace('/CON\b/','COM', $nomeLimpo);
    $nomeLimpo = preg_replace('/TON\b/','TOM', $nomeLimpo);
    
11.08.2016 / 01:05
0

Good guy, with regex , it makes more sense to do that \w* schema, so you can type any character as many times as you want, if it is just name [a-zA-Z] but if it is specifically the end of the name, put [a-zA-Z]*(com|con) < - those in the case of Maicon and Wellington

    
11.08.2016 / 00:26
0

Example with str_ireplace ()

$str = 'あTON, いCON, うTON';
echo str_ireplace(array('con,', 'ton,'), array('com', 'tom'), $str.',');

note: It will work if the original string always has the same pattern without spacing before the separator character. In your case, comma.

You can also choose str_replace () . The difference is that str_ireplace() is case-insensitive, ie it ignores lower case and upper case.

    
11.08.2016 / 03:32