I want to change the letter a in the word to o, but not all the letter a, in my rule, only the letters a in the words ending with a should be changed to o, but all the letters a are changing to the letter o, kak kok . kaka koko is happening. kak does not end with a, but the letter a in the middle is changing to the letter o. I need the kak kak state to be in the kaka koko state below.
<?php
$str = "kak kaka";
$searchVal = array("a");
$replaceVal = array("o");
$res = str_replace($searchVal, $replaceVal, $str);
print_r($res);
?>
type here
I didn’t know what to try.
2
Answers
If my assumption in the comments was correct, this will do it:
Output:
You haven’t added any logic to check for last character validation. For this, you can use
preg_replace_callback
one line solution to match and filter only those words that end witha
and then usestr_replace
to replace alla
witho
.Snippet:
Online Demo