PHP Remove specific space from string

Issue

$destination ="Hong Kong " 

I want to remove the last space to become like this

$destination ="Hong Kong" 

Solution

You can use trim, ltrim or rtrim function.

trim removes the characters at the beginning and end of the string.
ltrim, only those at the beginning of the string.
and rtrim those at the end of the string.

In your case to delete the last space, you can do so :

$destination = rtrim("Hong Kong "); //prints "Hong Kong"

Answered By – Jedupont

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published