How do I remove extra spaces at the end of a string using regex (preg_replace)?
$string = "some random text with extra spaces at the end ";
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no need of regex here and you can use
rtrimfor it, its cleaner and faster:But if you want a regex based solution you can use:
The regex used is
/\s*$/\sis short for any white spacechar, which includes space.
*is the quantifier for zero ormore
$is the end anchorBasically we replace trailing whitespace characters with nothing (
''), effectively deleting them.