Removing non-printable characters
Posted: Wed Jun 11, 2008 4:48 pm
What is the best way to remove non-printable characters in the middle of a string (whitespace)? Trim() is great for the ends.
Todd
Todd
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
No... trim() works from the ends of the string towards the center, but once it runs out of valid characters to trim, it stops.hansford wrote:trim() is good for the whole thing as you can add a character list as one of the parameters and remove those as well
hansford wrote:$newstring = preg_replace("/[\n\r\t]/","",$subject);
Code: Select all
$newString = preg_replace('~\s+~', '', $subject);Using the '\s' regex accounts for all whitespace, including hidden characters. If you use str_replace, you need to be aware of all of the possible whitespace characters. '\s' just simplifies it for the developer, in my opinion.lonelywolf wrote:why we don't use str_replace()?