How to change the end of a name

Asked

Viewed 50 times

3

How to change the end of a name for example

WELITON, MAICON, WELIGTON

for

WELITOM, MAICOM, WELIGTOM

5 answers

2

After you create the regex pattern, use the preg_replace function to do the substitution. It should be passed as the default parameter to find the characters you want to replace, the new characters and the text that will be changed. http://php.net/manual/en/function.preg-replace.php

1

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

  • that part I had already understood but I want you not to find the words with ton|con in the middle for example content or tesCONtes

  • how to find in the middle ?

  • This way you also find the words with "CON|TON" in the middle of it, I want to find only when the "CON|TON" is at the end

  • tries the [con|ton]{1} so it will have to have one of the two at the end

1

got it this way

    $nomeLimpo = preg_replace('/CON\b/','COM', $nomeLimpo);
    $nomeLimpo = preg_replace('/TON\b/','TOM', $nomeLimpo);

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 version with reduced code:

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

Utilizing:

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

Upshot:

WELITOM

MAICOM

WELIGTOM

CONRRADO

I hope I’ve helped!

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 to str_replace(). The difference is that str_ireplace() is case-insensitive, ie ignores lowercase and uppercase letter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.