Skip to content

Commit

Permalink
use php83 mb_str_pad() for Str::pad* (#49108)
Browse files Browse the repository at this point in the history
  • Loading branch information
amacado committed Nov 23, 2023
1 parent 0b475bb commit f0ac18b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,10 @@ public static function matchAll($pattern, $subject)
*/
public static function padBoth($value, $length, $pad = ' ')
{
if (function_exists('mb_str_pad')) {
return mb_str_pad($value, $length, $pad, STR_PAD_BOTH);
}

$short = max(0, $length - mb_strlen($value));
$shortLeft = floor($short / 2);
$shortRight = ceil($short / 2);
Expand All @@ -769,6 +773,10 @@ public static function padBoth($value, $length, $pad = ' ')
*/
public static function padLeft($value, $length, $pad = ' ')
{
if (function_exists('mb_str_pad')) {
return mb_str_pad($value, $length, $pad, STR_PAD_LEFT);
}

$short = max(0, $length - mb_strlen($value));

return mb_substr(str_repeat($pad, $short), 0, $short).$value;
Expand All @@ -784,6 +792,10 @@ public static function padLeft($value, $length, $pad = ' ')
*/
public static function padRight($value, $length, $pad = ' ')
{
if (function_exists('mb_str_pad')) {
return mb_str_pad($value, $length, $pad, STR_PAD_RIGHT);
}

$short = max(0, $length - mb_strlen($value));

return $value.mb_substr(str_repeat($pad, $short), 0, $short);
Expand Down

0 comments on commit f0ac18b

Please sign in to comment.