note 99732 added to function.sprintf
| From: | dwieeb at gmail dot com | Date: | Wed, 01 Sep 2010 18:54:48 +0000 |
| Subject: | note 99732 added to function.sprintf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172251@lists.php.net to get a copy of this message | ||
If you use the default padding specifier (a space) and then print it to HTML, you will notice that
HTML does not display the multiple spaces correctly. This is because any sequence of white-space is
treated as a single space.
To overcome this, I wrote a simple function that replaces all the spaces in the string returned by
sprintf() with the character entity reference " " to achieve non-breaking space
in strings returned by sprintf()
<?php
//Here is the function:
function sprintf_nbsp() {
$args = func_get_args();
return str_replace(' ', ' ', vsprintf(array_shift($args),
array_values($args)));
}
//Usage (exactly like sprintf):
$format = 'The %d monkeys are attacking the [%10s]!';
$str = sprintf_nbsp($format, 15, 'zoo');
echo $str;
?>
The above example will output:
The 15 monkeys are attacking the [ zoo]!
<?php
//The variation that prints the string instead of returning it:
function printf_nbsp() {
$args = func_get_args();
echo str_replace(' ', ' ', vsprintf(array_shift($args),
array_values($args)));
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 137.104.110.166
----
Manual Page -- http://www.php.net/manual/en/function.sprintf.php
Edit -- https://master.php.net/note/edit/99732
Del: integrated -- https://master.php.net/note/delete/99732/integrated
Del: useless -- https://master.php.net/note/delete/99732/useless
Del: bad code -- https://master.php.net/note/delete/99732/bad+code
Del: spam -- https://master.php.net/note/delete/99732/spam
Del: non-english -- https://master.php.net/note/delete/99732/non-english
Del: in docs -- https://master.php.net/note/delete/99732/in+docs
Del: other reasons-- https://master.php.net/note/delete/99732
Reject -- https://master.php.net/note/reject/99732
Search -- https://master.php.net/manage/user-notes.php