parent
206fb555d1
commit
1d5d2eee7c
@ -1,3 +1,6 @@
|
||||
# XXX
|
||||
# jrosset/mbstring-extended
|
||||
|
||||
XXX
|
||||
Add “missing” mbstring methods.
|
||||
|
||||
Provides:
|
||||
- [mb_str_pad](https://www.php.net/manual/function.str-pad.php)
|
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
use jrosset\MbstringExtended;
|
||||
|
||||
if (!function_exists('mb_str_pad')) {
|
||||
/**
|
||||
* Multi-bytes implementation for {@see https://www.php.net/manual/function.str-pad.php str_pad}
|
||||
*
|
||||
* @param string $string The input string
|
||||
* @param int $length The minimum string length
|
||||
* @param string $pad_string The padding string
|
||||
* @param int $pad_type STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH
|
||||
* @param string|null $encoding The character encoding or the internal character encoding value is Null
|
||||
*
|
||||
* @return string The padded string
|
||||
*/
|
||||
function mb_str_pad (string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, string $encoding = null): string {
|
||||
return MbstringExtended::str_pad($string, $length, $pad_string, $pad_type, $encoding);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace jrosset;
|
||||
|
||||
/**
|
||||
* Class for “missing” mbstring functions
|
||||
*/
|
||||
final class MbstringExtended {
|
||||
/**
|
||||
* Multi-bytes implementation for {@see https://www.php.net/manual/function.str-pad.php str_pad}
|
||||
*
|
||||
* @param string $string The input string
|
||||
* @param int $length The minimum string length
|
||||
* @param string $pad_string The padding string
|
||||
* @param int $pad_type STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH
|
||||
* @param string|null $encoding The character encoding or the internal character encoding value is Null
|
||||
*
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function str_pad (string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, string $encoding = null): string {
|
||||
$diff = strlen($string) - ($encoding === null ? mb_strlen($string) : mb_strlen($string, $encoding));
|
||||
return str_pad($string, $length + $diff, $pad_string, $pad_type);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
echo mb_str_pad('𝄞', 20, '=', STR_PAD_RIGHT, 'UTF-8');
|
Loading…
Reference in New Issue