Let’s say I got this string:
$str = "alemylaife";
(I know it’s misspelled, all my life)
Now how can I replace
“ale” without replacing whatever comes after “ale” that also contains the same letters?
Let’s say I got this arrays:
$replace = Array("a","l","e");
$with = Array("wh","at","ever");
$str = str_replace($replace,$with,$str);
echo $str;
this will print:
whatevermyatwhifever
How could I stop the str_replace for replacing the letters after “ale”?
Maybe I got to trim the 3 first letters then replacing them then join the trimmed strings?
EDIT: I’m going to try to express myself clearer.
I want only the three (3) first letters to be replaced IF they contain A THEN L THEN whateverletter so, ala ale ald alr aly. Those are the letters that I want to be replaced. I don’t want letters after them to be affected.
Question isn’t really clear. Assuming you want to replace “ale” with “whatever” (correct me if I’m wrong), then you could use preg_replace to limit the number of replacements.
Example 1: (replace the first occurrence of “ale”)
Example 2: (replace only if the string starts with “ale”)
EDIT: You edited your question to provide better details, I would use: