Before I get back on the road yet again, here’s a quick little (horrible) PHP snippit. A friend of mine is learning PHP, and asked me how to strip all of the ‘unprintable’ characters out of a string. ctype_alpha() can not be considered portable enough yet, and of course, there is no isascii() in PHP… not to mention that PHP’s string manipulation is haphazard and slow – at best.
Here’s what I ended up giving him. It’s a trivial little hack that accepts two variables; a string (of course), and a boolean, “dots” to replace unprintable characters with periods. It’s highly tied to the ASCII character set.
function nonascii_string_cleaning($myText, $dots = FALSE) { $trans_array = array(); // This is the lower (unprintable) ASCII character set for ($i = 0; $i < 32; $i++) { $trans_array[chr($i)] = ($dots) ? ”.” : “”; } // This is the “higher” (unprintable) ASCII character set for ($i = 129; $i < 255; $i++) { $trans_array[chr($i)] = ($dots) ? ”.” : “”; } $replaced = strtr($myText, $trans_array); return $replaced; }Horrid Example (Yes, this is what he wanted to do:)
It’s pretty simple, if not mostly-useless. Still, this makes me wish there were more C-like functions in PHP. Come on, guys! This would have been much better with isalpha() and an intelligent getchar() type of ordeal. Sure, it’s possible to use a file handle, or pass things as an array; but with my testing, and his requirement of it accepting a string, this came out about as fast and ‘bare wire’ as possible, while still being legible. Note my use of the ternary operators. ;)
[Edit: forgot to close a span tag. Oops.]