Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace tabs or multiple spaces

I just wanted to check this code was valid for what I am trying to do (replaces tab(s) or multiple spaces with one space.

preg_replace('/\t+|\s{2,}/', ' ', $street);

However what if it found a tab AND a space together, then wouldn't we end up with two spaces then? The original space & the new one the tab got replaced with.

How can I change it so any spaces in the string always end up only being the one space?

like image 474
Brett Avatar asked May 09 '13 15:05

Brett


1 Answers

As \s will match both tabs and spaces (as well as other forms of white-space), this itself should do the trick to replace any amount of consecutive white-space with a single space:

preg_replace('/\s+/', ' ', $street);
like image 59
Dallas Avatar answered Oct 23 '22 23:10

Dallas