note 94707 deleted from function.str-pad by danbrown
| From: | danbrown@php.net | Date: | Sat, 02 Jul 2011 18:52:29 +0000 |
| Subject: | note 94707 deleted from function.str-pad by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181186@lists.php.net to get a copy of this message | ||
Note Submitter: shelby at coolpage dot com
----
Convenience functions (perhaps these should be added as native functions if popular?):
<?php
function str_lpad( &$s, &$l, &$p = ' ' ) { return str_pad( $s, $l, $p,
STR_PAD_LEFT ); }
function str_nlpad( &$s, &$l ) { return str_lpad( $s, $l, '0' ); }
function str_npad( &$s, &$l ) { return str_pad( $s, $l, '0' ); }
?>
E.g.:
<?php
$mystr = str_nlpad( $mystr, 4 )
?>
Are more concise, semantically coherent, and perhaps also more efficient because do not create an
intermediate discarded string, than the clever:
http://www.php.net/manual/en/function.str-pad.php#14585
<?php
$mystr = substr("0000$mystr",-4)
?>