Removing non-printable characters
Moderator: General Moderators
Removing non-printable characters
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
Re: Removing non-printable characters
I'd probably use preg_replace() or str_replace()
Re: Removing non-printable characters
trim() is good for the whole thing as you can add a character list as one of the parameters and remove those as well
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Removing non-printable characters
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
Re: Removing non-printable characters
I stand corrected. My appologies. Possibly this?
$newstring = preg_replace("/[\n\r\t]/","",$subject);
$newstring = preg_replace("/[\n\r\t]/","",$subject);
Re: Removing non-printable characters
Thank you all...very helpful!
Todd
Todd
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Removing non-printable characters
hansford wrote:$newstring = preg_replace("/[\n\r\t]/","",$subject);
Code: Select all
$newString = preg_replace('~\s+~', '', $subject);- lonelywolf
- Forum Commoner
- Posts: 28
- Joined: Tue Jun 10, 2008 6:15 am
Re: Removing non-printable characters
why we don't use str_replace()?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Removing non-printable characters
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()?