Created
November 4, 2009 20:30
-
-
Save nebiros/226350 to your computer and use it in GitHub Desktop.
mb_str_pad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* mb_str_pad | |
* | |
* @param string $input | |
* @param int $pad_length | |
* @param string $pad_string | |
* @param int $pad_type | |
* @return string | |
* @author Kari "Haprog" Sderholm | |
*/ | |
function mb_str_pad( $input, $pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT) | |
{ | |
$diff = strlen( $input ) - mb_strlen( $input ); | |
return str_pad( $input, $pad_length + $diff, $pad_string, $pad_type ); | |
} |
bad idea as unless the whole string is multi-byte this could lead to issues with string encoding...
Works for me.
Works for me too...
This is the behavior I expected from str_pad.
My variant (https://gist.github.com/rquadling/c9ff12755fc412a6f0d38f6ac0d24fb1), with unit test
https://gist.github.com/rquadling/c9ff12755fc412a6f0d38f6ac0d24fb1
Thanks!
Hope it works well for you.
Thanks man. 2 hours with this problem.
Thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If your pad_string is itself multibyte, this still won't give the right result. The tricky part is that str_pad will copy as many bytes out of the pad_string as it needs to reach the pad_length, and will therefore generate corrupt strings. So, basically, you cannot use str_pad unless you first make sure you append too many bytes and then copy the right length out of it via mb_substr.