Mark
Mark

Reputation: 812

PHP - Remove all tabs from string

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?

Upvotes: 38

Views: 85728

Answers (5)

levi-jcbs
levi-jcbs

Reputation: 90

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

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

Upvotes: 3

Mirko Pagliai
Mirko Pagliai

Reputation: 1241

trim(preg_replace('/[\t|\s{2,}]/', '', $result))

Removes all tabs, including tabs created with multiple spaces

Upvotes: 3

Amani Ben Azzouz
Amani Ben Azzouz

Reputation: 2565

this will remove all tabs in your variable $string

preg_replace('/\s+/', '', $string);

Upvotes: 8

Zamicol
Zamicol

Reputation: 5024

trim(preg_replace('/\t+/', '', $string))

Upvotes: 81

Mr. Alien
Mr. Alien

Reputation: 157334

Try using this regular expression

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

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

Upvotes: 11

Related Questions