Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim end of string by pattern (regex?)

I'm not so good at regex..

Looking for a way to trim the end of a string, if the end is:

A space followed by ONE character (can be any character)

Is preg_replace() the thing for that, or is there an easier and faster way?

The trimming will only happen once in the page

like image 277
mowgli Avatar asked Mar 19 '23 07:03

mowgli


1 Answers

For just a space do this. And yes preg_replace is the way to go.

$string = preg_replace('/ .$/', '', $string);
like image 81
colburton Avatar answered Mar 28 '23 09:03

colburton