PHP – Remove all tabs from string-ThrowExceptions

Exception or error:

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?

How to solve:
trim(preg_replace('/\t+/', '', $string))

Answer:

Try using this regular expression

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

This will trim out all tabs from the string …

Answer:

this will remove all tabs in your variable $string

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

Answer:

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

Removes all tabs, including tabs created with multiple spaces

Leave a Reply

Your email address will not be published. Required fields are marked *