38

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?

5
  • possible duplicate of remove multiple whitespaces in php
    – Kermit
    Commented Jan 29, 2013 at 16:02
  • 1
    I'm not the best with RegExp but you can specify 1 tab or more with + sign
    – poudigne
    Commented Jan 29, 2013 at 16:02
  • Seems to work for me: codepad.org/2u9ym1zB
    – gen_Eric
    Commented Jan 29, 2013 at 16:10
  • 2
    Just use preg_replace('/\t+/', '', $string)..
    – Kerem
    Commented Jan 29, 2013 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); to replace either tab or 4 spaces together, the + is not needed unless start ^ or end $ of pattern is also used.
    – Will B.
    Commented Mar 27, 2023 at 22:33

5 Answers 5

79
trim(preg_replace('/\t+/', '', $string))
3
  • 2
    Although similar to the post by Mr. Alien (which did not work), this one worked.
    – SamV
    Commented Aug 7, 2013 at 16:21
  • Watch out, the trim() part removes empty lines in the string if they appear in the beginning or the end.
    – Avatar
    Commented Nov 14, 2022 at 8: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
    Commented Nov 14, 2022 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 ...

6
  • 1
    A tab is white space; but white space isn't always a tab
    – Mark Baker
    Commented Jan 29, 2013 at 16:03
  • @MarkBaker According to you I should remove the g part?
    – Mr. Alien
    Commented Jan 29, 2013 at 16:05
  • 3
    no, the g part has absolutely no meaning.... I'm saying that \s includes spaces as well as tabs; OP is specifically asking about tabs
    – Mark Baker
    Commented Jan 29, 2013 at 16:09
  • @MarkBaker Ya I got it, I thought you are telling me to remove the global
    – Mr. Alien
    Commented Jan 29, 2013 at 16:11
  • 1
    The g flag does not exist in PHP's PCRE implementation. The replace is always performed globally. Your solution as-is doesn't work!
    – Zuul
    Commented May 19, 2015 at 12:14
9

this will remove all tabs in your variable $string

preg_replace('/\s+/', '', $string);
3
  • 4
    Wouldn't this just remove all spaces from a string as well?
    – scrowler
    Commented Apr 7, 2015 at 0:32
  • \s is a shorthand for [\t\r\n\f]. It matches new line, return and form feed as well as tabulator. Commented Nov 26, 2015 at 10:14
  • replace any white space [\t\r\n\f] with single space, preg_replace('/\s+/', ' ', trim($string));
    – zzapper
    Commented Dec 20, 2017 at 12:15
4
trim(preg_replace('/[\t|\s{2,}]/', '', $result))

Removes all tabs, including tabs created with multiple spaces

1
  • 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
    Commented Aug 31, 2018 at 13:30
3

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.