10

I'm trying to replace the last occurence of a comma in a text with "and" using strrchr() and str_replace().

Example:

$likes = 'Apple, Samsung, Microsoft';
$likes = str_replace(strrchr($likes, ','), ' and ', $likes);

But this replaces the entire last word (Microsoft in this case) including the last comma in this string. How can I just remove the last comma and replace it with " and " ?

I need to solve this using strrchr() as a function. That's why this question is no duplicate and more specific.

3
  • 2
    Possible duplicate of PHP Replace Last Occurence of a String in a String? Commented Jan 20, 2017 at 11:47
  • 1
    I saw that question. I need to solve this using strrchr. Commented Jan 20, 2017 at 11:58
  • @Thamilhan You should actually read the question first before reporting everything as duplicate... Commented Apr 2, 2023 at 17:57

4 Answers 4

12

To replace only the last occurrence, I think the better way is:

$likes = 'Apple, Samsung, Microsoft';
$likes = substr_replace($likes, ' and', strrpos($likes, ','), 1);

strrpos finds the position of last comma, and substr_replace puts the desired string in that place replacing '1' characters in this case.

Sign up to request clarification or add additional context in comments.

2 Comments

If $likes='Apple' , this function returns ' andpple'
I don't understand why this is the accepted answer, when the above comment applies, as I have also verified.
5

You can use regex to find last comma in string. Php preg_replace() replace string with another string by regex pattern.

$likes = 'Apple, Samsung, Microsoft';
$likes = preg_replace("/,([^,]+)$/", " and $1", $likes)

Check result in demo

1 Comment

you can also do preg_replace('/,(?=[^,]*$)/',' and', $likes);
4

just provide the answer with function strrchr()

$likes = 'Apple, Samsung, Microsoft';
$portion = strrchr($likes, ',');
$likes = str_replace($portion, (" and" . substr($portion, 1, -1)), $likes);

because strrchr() will

This function returns the portion of string

See Doc here

so we just need only replace the comma symbol should be fine. and the comma will be always the first character when you use strrchr()

See Demo here

Comments

1

first, you gotta separate the elements into an array with all but the last one, and the last one. then you put them back together with commas and an "and", respectively

$likes = "A, B, C";
$likes_arr = explode(",", $likes);
$last = array_pop($likes_arr);
$likes = implode(",", $likes_arr) . " and" . $last;
echo $likes; //"A, B and C";

however: don't forget to check if you actually have enough elements. this fails for inputs without comma.

3 Comments

Warning: array_pop() expects parameter 1 to be array
We can use if($last) { $likes = implode(",", $likes_arr) . " and" . $last; echo $likes; //"A, B and C"; } for fails of input without comma
@RajeshBaskaran no. not in that way, because for the input "A", $likes_arr becomes ["A"], so $last will be "A" which evaluates to true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.