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?
The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.
$str = "This text"; $str = preg_replace('/\\s/','',$str); echo $str; That would be a good method to use if you want to remove other characters, such a punctuation, from a string aswell. You would need to construct a regular expression mind you.
Answer: Use the PHP str_replace() Function You can simply use the PHP str_replace() function to strip or remove all spaces inside a string.
trim(preg_replace('/\t+/', '', $string))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With