Skip to content

Instantly share code, notes, and snippets.

@rquadling
Last active September 5, 2023 09:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rquadling/c9ff12755fc412a6f0d38f6ac0d24fb1 to your computer and use it in GitHub Desktop.
Save rquadling/c9ff12755fc412a6f0d38f6ac0d24fb1 to your computer and use it in GitHub Desktop.
Implementation of a multi-byte equivalent of PHP's str_pad function.
/**
* Multibyte String Pad
*
* Functionally, the equivalent of the standard str_pad function, but is capable of successfully padding multibyte strings.
*
* @param string $input The string to be padded.
* @param int $length The length of the resultant padded string.
* @param string $padding The string to use as padding. Defaults to space.
* @param int $padType The type of padding. Defaults to STR_PAD_RIGHT.
* @param string $encoding The encoding to use, defaults to UTF-8.
*
* @return string A padded multibyte string.
*/
function mb_str_pad($input, $length, $padding = ' ', $padType = STR_PAD_RIGHT, $encoding = 'UTF-8')
{
$result = $input;
if (($paddingRequired = $length - mb_strlen($input, $encoding)) > 0) {
switch ($padType) {
case STR_PAD_LEFT:
$result =
mb_substr(str_repeat($padding, $paddingRequired), 0, $paddingRequired, $encoding).
$input;
break;
case STR_PAD_RIGHT:
$result =
$input.
mb_substr(str_repeat($padding, $paddingRequired), 0, $paddingRequired, $encoding);
break;
case STR_PAD_BOTH:
$leftPaddingLength = floor($paddingRequired / 2);
$rightPaddingLength = $paddingRequired - $leftPaddingLength;
$result =
mb_substr(str_repeat($padding, $leftPaddingLength), 0, $leftPaddingLength, $encoding).
$input.
mb_substr(str_repeat($padding, $rightPaddingLength), 0, $rightPaddingLength, $encoding);
break;
}
}
return $result;
}
/**
* @dataProvider provideDataForMbStrPad
*
* @param string $input
* @param int $length
* @param string $padding
* @param int $padType
* @param string $result
*/
public function testMbStrPad($input, $length, $padding, $padType, $result)
{
$this->assertEquals($result, Strings::mbStrPad($input, $length, $padding, $padType));
}
public function provideDataForMbStrPad()
{
return [
['Nhiều byte string đệm', 0, ' ', STR_PAD_BOTH, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 0, ' ', STR_PAD_LEFT, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 0, ' ', STR_PAD_RIGHT, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 0, '充', STR_PAD_BOTH, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 0, '充', STR_PAD_LEFT, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 0, '充', STR_PAD_RIGHT, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 0, '煻充', STR_PAD_BOTH, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 20, ' ', STR_PAD_BOTH, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 20, ' ', STR_PAD_LEFT, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 20, ' ', STR_PAD_RIGHT, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 20, '充', STR_PAD_BOTH, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 20, '充', STR_PAD_LEFT, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 20, '充', STR_PAD_RIGHT, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 20, '煻充', STR_PAD_BOTH, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 21, ' ', STR_PAD_BOTH, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 21, ' ', STR_PAD_LEFT, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 21, ' ', STR_PAD_RIGHT, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 21, '充', STR_PAD_BOTH, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 21, '充', STR_PAD_LEFT, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 21, '充', STR_PAD_RIGHT, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 21, '煻充', STR_PAD_BOTH, 'Nhiều byte string đệm'],
['Nhiều byte string đệm', 22, ' ', STR_PAD_BOTH, 'Nhiều byte string đệm '],
['Nhiều byte string đệm', 22, ' ', STR_PAD_LEFT, ' Nhiều byte string đệm'],
['Nhiều byte string đệm', 22, ' ', STR_PAD_RIGHT, 'Nhiều byte string đệm '],
['Nhiều byte string đệm', 22, '充', STR_PAD_BOTH, 'Nhiều byte string đệm充'],
['Nhiều byte string đệm', 22, '充', STR_PAD_LEFT, '充Nhiều byte string đệm'],
['Nhiều byte string đệm', 22, '充', STR_PAD_RIGHT, 'Nhiều byte string đệm充'],
['Nhiều byte string đệm', 22, '煻充', STR_PAD_BOTH, 'Nhiều byte string đệm煻'],
['Nhiều byte string đệm', 23, ' ', STR_PAD_BOTH, ' Nhiều byte string đệm '],
['Nhiều byte string đệm', 23, ' ', STR_PAD_LEFT, ' Nhiều byte string đệm'],
['Nhiều byte string đệm', 23, ' ', STR_PAD_RIGHT, 'Nhiều byte string đệm '],
['Nhiều byte string đệm', 23, '充', STR_PAD_BOTH, '充Nhiều byte string đệm充'],
['Nhiều byte string đệm', 23, '充', STR_PAD_LEFT, '充充Nhiều byte string đệm'],
['Nhiều byte string đệm', 23, '充', STR_PAD_RIGHT, 'Nhiều byte string đệm充充'],
['Nhiều byte string đệm', 23, '煻充', STR_PAD_BOTH, '煻Nhiều byte string đệm煻'],
['Nhiều byte string đệm', 24, ' ', STR_PAD_BOTH, ' Nhiều byte string đệm '],
['Nhiều byte string đệm', 24, ' ', STR_PAD_LEFT, ' Nhiều byte string đệm'],
['Nhiều byte string đệm', 24, ' ', STR_PAD_RIGHT, 'Nhiều byte string đệm '],
['Nhiều byte string đệm', 24, '充', STR_PAD_BOTH, '充Nhiều byte string đệm充充'],
['Nhiều byte string đệm', 24, '充', STR_PAD_LEFT, '充充充Nhiều byte string đệm'],
['Nhiều byte string đệm', 24, '充', STR_PAD_RIGHT, 'Nhiều byte string đệm充充充'],
['Nhiều byte string đệm', 24, '煻充', STR_PAD_BOTH, '煻Nhiều byte string đệm煻充'],
['Nhiều byte string đệm', 25, '煻充', STR_PAD_BOTH, '煻充Nhiều byte string đệm煻充'],
['Nhiều byte string đệm', 26, '煻充', STR_PAD_BOTH, '煻充Nhiều byte string đệm煻充煻'],
];
}
@ian-maurmann
Copy link

ian-maurmann commented Sep 4, 2023

First-off, thank you for making this!

I was having some issues with trying to pad some of the IPA alphabet diacritics, and after some googl'ing around I found your code here. I still had a few issues with the some of the IPA diacritics, but swapping mb_strlen on line 17 with grapheme_strlen fixed it for me.

(grapheme_strlen only works when $encoding === 'UTF-8', so I'm not sure you'd want to swap it here) but that's what worked for me!

@rquadling
Copy link
Author

Can you give me some examples of the strings you are dealing with? I do only ever expect to work in UTF-8, but I wrote the code as a match to str_pad but with the mb_ ability.

I guess, an example line for the provider would be the best. What SHOULD the line look like when using your content.

@rquadling
Copy link
Author

I'd probably also suggest replace mb_substr with grapheme_substr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment