35

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
  • 778
  • 2
  • 11
  • 21
  • possible duplicate of [remove multiple whitespaces in php](http://stackoverflow.com/questions/2326125/remove-multiple-whitespaces-in-php) – Kermit Jan 29 '13 at 16:02
  • 1
    I'm not the best with RegExp but you can specify 1 tab or more with + sign – poudigne Jan 29 '13 at 16:02
  • Seems to work for me: http://codepad.org/2u9ym1zB – gen_Eric Jan 29 '13 at 16:10
  • 2
    Just use `preg_replace('/\t+/', '', $string)`.. – Kerem Jan 29 '13 at 16:53
  • There seems to be a misunderstanding, the question is related to the `| |` (tab) character but `| |` (4 spaces) characters were supplied in the example code. Since the source `$txt` value that was causing the issue was not provided, we can't verify the issue. The original code would have removed double tabs as 8 spaces as well, so seems OP may be wanting something like [`preg_replace('/\t| {4}/', '', $txt);`](https://3v4l.org/XcEAW) to replace either tab or 4 spaces together, the `+` is not needed unless start `^` or end `$` of pattern is also used. – Will B. Mar 27 '23 at 22:33

5 Answers5

78
trim(preg_replace('/\t+/', '', $string))
Zamicol
  • 4,626
  • 1
  • 37
  • 42
  • 2
    Although similar to the post by Mr. Alien (which did not work), this one worked. – SamV Aug 07 '13 at 16:21
  • Watch out, the `trim()` part removes empty lines in the string if they appear in the beginning or the end. – Avatar Nov 14 '22 at 08:30
  • Yes, you can just do `preg_replace('/\t+/', '', $string)`, but I typically found it useful to remove leading and ending white space as well. – Zamicol Nov 14 '22 at 17:58
11

Try using this regular expression

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

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

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
7

this will remove all tabs in your variable $string

preg_replace('/\s+/', '', $string);
Amani Ben Azzouz
  • 2,477
  • 3
  • 15
  • 26
4
trim(preg_replace('/[\t|\s{2,}]/', '', $result))

Removes all tabs, including tabs created with multiple spaces

Mirko Pagliai
  • 1,220
  • 1
  • 19
  • 36
  • 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
  • 72
  • 4