note 120298 added to function.trim

From: Date: Thu, 08 Dec 2016 09:32:29 +0000
Subject: note 120298 added to function.trim
Groups: php.notes 
Request: Send a blank email to php-notes+get-208277@lists.php.net to get a copy of this message
PHP's "magic byte" for non-ASCII characters may become anoying when you want to find out if the input string is multi-byte or not (means pure ASCII without any non-ASCII characters). trim() by default does not remove it from the string, so you may want to do it on your own: <?php // Trims string including "space" character and the "magic byte" for non-ASCII characters function trimString ($str) { // Trim away all unwanted characters (including default) $trimmed = trim($str," \t\n\r\0\x0B\x0A\xc3"); // Return finished string return $trimmed; } ?> Usage: (save file as UTF-8!) <?php $str = 'ä'; // For console/prompt: print 'str=0x' . bin2hex($str) . PHP_EOL; print 'trim=0x' . bin2hex($trim) . PHP_EOL; print 'trimString=0x' . bin2hex($trimString) . PHP_EOL; print 'length after trim()=' . strlen($trim) . PHP_EOL; print 'length after trimString()=' . strlen($trimString) . PHP_EOL; ?> Output: str=0xc3a4 trim=0xc3a4 trimString=0xa4 length after trim()=2 length after trimString()=1 bin2hex() is being used here to show that 0xc3 really exists as first byte. You can test it with any other multi-byte character. ---- Server IP: 213.172.110.13 Probable Submitter: 212.93.31.219 ---- Manual Page -- http://php.net/manual/en/function.trim.php Edit -- https://master.php.net/note/edit/120298 Del: integrated -- https://master.php.net/note/delete/120298/integrated Del: useless -- https://master.php.net/note/delete/120298/useless Del: bad code -- https://master.php.net/note/delete/120298/bad+code Del: spam -- https://master.php.net/note/delete/120298/spam Del: non-english -- https://master.php.net/note/delete/120298/non-english Del: in docs -- https://master.php.net/note/delete/120298/in+docs Del: other reasons-- https://master.php.net/note/delete/120298 Reject -- https://master.php.net/note/reject/120298 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#208277) next »