PHP offers the str_pad
function, which can be used to pad a string to a specified length with another string. This function is particularly useful for formatting and aligning strings in various applications, such as creating fixed-width columns or ensuring consistent string lengths in output. However, the str_pad
function does not have support for multibyte characters, leading to issues when dealing with languages employing multibyte encodings such as UTF-8.
Since PHP 8.3, the mb_str_pad
function has become available. This function can be utilized to pad a multibyte string to a specified length with another multibyte string, addressing the previous limitation of the str_pad
function in handling multibyte character encodings like UTF-8.
<?php
echo mb_str_pad('卞卞', 6, '义', STR_PAD_BOTH); // 义义卞卞义义
Leave a Comment
Cancel reply