note 86304 added to function.sprintf

From: Date: Sun, 12 Oct 2008 11:29:10 +0000
Subject: note 86304 added to function.sprintf
Groups: php.notes 
Request: Send a blank email to php-notes+get-145284@lists.php.net to get a copy of this message
If you have problems using utf-8 with string paddings, you can use this function: <?php /** * Formats string using sprintf, but correctly handles %s with space paddings * * uses conversion to iso-8859-2 and back * * it is, however, much slower, so use only when needed! * * @param string $format */ function utf_8_sprintf ($format) { $args = func_get_args(); for ($i = 1; $i < count($args); $i++) { $args [$i] = iconv('UTF-8', 'ISO-8859-2', $args [$i]); } return iconv('ISO-8859-2', 'UTF-8', call_user_func_array('sprintf', $args)); } // test echo sprintf ("[%-20s]\n", 'escrzyaie'); // how it should look without special chars echo sprintf ("[%-20s]\n", 'ěščřžýáíé'); // not correctly handled by php echo utf_8_sprintf ("[%-20s]\n", 'ěščřžýáíé'); // using above function /* produces: [escrzyaie ] [ěščřžýáíé ] [ěščřžýáíé ] */ ?> the trick is to convert arguments to iso8859-2 and result back to utf-8. ---- Server IP: 147.32.160.17 Probable Submitter: 88.146.45.50 ---- Manual Page -- http://www.php.net/manual/en/function.sprintf.php Edit -- https://master.php.net/note/edit/86304 Del: integrated -- https://master.php.net/note/delete/86304/integrated Del: useless -- https://master.php.net/note/delete/86304/useless Del: bad code -- https://master.php.net/note/delete/86304/bad+code Del: spam -- https://master.php.net/note/delete/86304/spam Del: non-english -- https://master.php.net/note/delete/86304/non-english Del: in docs -- https://master.php.net/note/delete/86304/in+docs Del: other reasons-- https://master.php.net/note/delete/86304 Reject -- https://master.php.net/note/reject/86304 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145284) next »