34

I am able to remove all single tabs from a string:

// Copying and pasting the tab directly
$txt = str_replace("    ", "", $txt); 

This only removes single tabs, but not double tabs. I then tried this, thinking that "\t" would be sufficient to find the tabs:

$txt = preg_replace('/\t/', '', $txt);

However, it didn't work. Can anyone offer something better?

Mark
  • 748
  • 2
  • 11
  • 19

5 Answers5

75
trim(preg_replace('/\t+/', '', $string))
Zamicol
  • 3,891
  • 1
  • 31
  • 36
11

Try using this regular expression

$string = trim(preg_replace('/\t/g', '', $string));

This will trim out all tabs from the string ...

Mr. Alien
  • 147,524
  • 33
  • 287
  • 271
7

this will remove all tabs in your variable $string

preg_replace('/\s+/', '', $string);
Amani Ben Azzouz
  • 2,447
  • 2
  • 13
  • 25
4
trim(preg_replace('/[\t|\s{2,}]/', '', $result))

Removes all tabs, including tabs created with multiple spaces

Mirko Pagliai
  • 1,171
  • 1
  • 17
  • 35
  • This also removes all regular spaces as well. i added a space between the single quotes and that worked perfectly for me. It replaced all tabs, spaces, etc with a single space. I was trying to get text into a csv. I needed to strip out all of the tabs, double spaces and whatever because it was triggering a new column. This worked for me. trim(preg_replace('/[\t|\s{2,}]/', ' ', $result)) – Robbiegod Aug 31 '18 at 13:30
3

$string = trim(preg_replace('/\t/', '', $string));

This works for me. Remove the g of @Mr. Aliens answer.

levi-jcbs
  • 52
  • 4