note 71548 added to function.str-pad
| From: | Charles at osu1 dot php dot net | Date: | Tue, 05 Dec 2006 16:11:45 +0000 |
| Subject: | note 71548 added to function.str-pad | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120307@lists.php.net to get a copy of this message | ||
Small correction to this otherwise good code (below). You must use mb_substr in case the pad-string
is multibyte-encoded. If this is assumed to never be the case, then the code is trivial:
<?php
// pad-string is never multibyte:
//
function m_str_pad($ps_input, $pn_pad_length, $ps_pad_string = " ", $pn_pad_type =
STR_PAD_RIGHT, $ps_encoding = NULL) {
if (is_null($ps_encoding))
$ps_encoding = mb_internal_encoding();
return str_pad($ps_input, $pn_pad_length + strlen($ps_input) - mb_strlen($ps_input, $ps_encoding),
$ps_pad_string, $pn_pad_type);
}
// pad-string possibly multibyte:
//
function m_str_pad($input, $pad_length, $pad_string = '', $pad_type = 1, $charset = null){
$str = '';
if ($charset==null)
$charset = mb_internal_encoding();
$length = $pad_length - mb_strlen($input, $charset);
$psLength = mb_strlen($pad_string); // pad string length
if ($psLength<=0 OR $length<=0){
// pad string equal 0
$str = $input;
}else{
// pad string AND space to padding is greater than 1
$repeatCount = floor($length/$psLength); // how many times repeat
$lastStr = substr($pad_string, 0, $length%$psLength); // last part of pad string
if($pad_type == STR_PAD_RIGHT){
$str = $input.str_repeat($pad_string, $repeatCount).$lastStr;
} elseif($pad_type == STR_PAD_LEFT){
$str = str_repeat($pad_string, $repeatCount).$lastStr.$input;
} elseif($pad_type == STR_PAD_BOTH){
$repeatCountLeft = 0;
$repeatCountRight = 0;
$lastStrLeft = '';
$lastStrRight = '';
if ($repeatCount%2==0){
// if is divisible 2
$repeatCountLeft = $repeatCountRight = floor($repeatCount/2);
}else{
// if not - has to dive to 2 same parts
$repeatCountLeft = $repeatCountRight = floor(($repeatCount-1)/2);
}
$lastStrLength = $length-2*$repeatCountLeft*$psLength; // the rest length to pad
$lastStrLeftLength = $lastStrRightLength = floor($lastStrLength/2); // ther rest length
divide to 2 parts
$lastStrRightLength += $lastStrLength%2; // the last char add to right side
$lastStrLeft = mb_substr($pad_string, 0, $lastStrLeftLength, $charset);
$lastStrRight = mb_substr($pad_string, 0, $lastStrRightLength, $charset);
$str = str_repeat($pad_string,$repeatCountLeft).$lastStrLeft;
$str .= $input;
$str .= str_repeat($pad_string,$repeatCountRight).$lastStrRight;
} else{
$str = str_repeat($pad_string, $repeatCount).$lastStr.$input;
}
}
return $str;
}
?>
----
Server IP: 83.138.144.80
Probable Submitter: 82.152.212.233
----
Manual Page -- http://www.php.net/manual/en/function.str-pad.php
Edit -- https://master.php.net/note/edit/71548
Del: integrated -- https://master.php.net/note/delete/71548/integrated
Del: useless -- https://master.php.net/note/delete/71548/useless
Del: bad code -- https://master.php.net/note/delete/71548/bad+code
Del: spam -- https://master.php.net/note/delete/71548/spam
Del: non-english -- https://master.php.net/note/delete/71548/non-english
Del: in docs -- https://master.php.net/note/delete/71548/in+docs
Del: other reasons-- https://master.php.net/note/delete/71548
Reject -- https://master.php.net/note/reject/71548
Search -- https://master.php.net/manage/user-notes.php