Changing the end of a name for example
WELITON, MAICON, WELIGTON
for
WELITOM, MAICOM, WELIGTOM
Changing the end of a name for example
WELITON, MAICON, WELIGTON
for
WELITOM, MAICOM, WELIGTOM
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!
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
I got it so
$nomeLimpo = preg_replace('/CON\b/','COM', $nomeLimpo);
$nomeLimpo = preg_replace('/TON\b/','TOM', $nomeLimpo);
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
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.