Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trimming a white space

Tags:

string

php

In a string that includes quotes, I always get an extra whitespace before the end quote. For instance

"this is a test " (string includes quotes)

Note the whitespace after test but before the end quote. How could I get rid of this space?

I tried rtrim but it just applies for chars at the end of the string, obviously this case is not at the end.

Any clues? Thanks


1 Answers

Here's another way, which only matches a sequence of spaces and a quote at the end of a string...

$str=preg_replace('/\s+"$/', '"', $str);
like image 158
Paul Dixon Avatar answered Jan 30 '26 20:01

Paul Dixon