라이브러리
[PHP] mb_str_pad - 멀티바이트 문자열을 다른 멀티바이트 문자열로 특정 길이만큼 채웁니다.
PHP의 mb_str_pad 함수
PHP의 `mb_str_pad` 함수는 문자열을 지정된 길이로 맞추기 위해 특정 문자를 왼쪽 또는 오른쪽에 추가하는 함수입니다. 이 함수는 멀티바이트 문자열을 지원하기 때문에, 한글이나 다른 언어를 지원하는 함수입니다.
함수 구조
`mb_str_pad` 함수의 구조는 다음과 같습니다.
#hostingforum.kr
php
string mb_str_pad ( string $str , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ] ) )
- `$str` : 문자열을 지정합니다.
- `$pad_length` : 문자열을 맞추기 위한 길이를 지정합니다.
- `$pad_string` : 추가할 문자열을 지정합니다. 기본값은 공백입니다.
- `$pad_type` : 추가할 위치를 지정합니다. 기본값은 오른쪽입니다. (STR_PAD_RIGHT), 왼쪽 (STR_PAD_LEFT), 또는 중간 (STR_PAD_BOTH)
예제
예제 1: 오른쪽에 공백 추가
#hostingforum.kr
php
$str = "Hello";
$pad_length = 10;
$pad_string = " ";
$pad_type = STR_PAD_RIGHT;
$result = mb_str_pad($str, $pad_length, $pad_string, $pad_type);
echo $result; // Hello
예제 2: 왼쪽에 공백 추가
#hostingforum.kr
php
$str = "Hello";
$pad_length = 10;
$pad_string = " ";
$pad_type = STR_PAD_LEFT;
$result = mb_str_pad($str, $pad_length, $pad_string, $pad_type);
echo $result; // Hello
예제 3: 중간에 공백 추가
#hostingforum.kr
php
$str = "Hello";
$pad_length = 10;
$pad_string = " ";
$pad_type = STR_PAD_BOTH;
$result = mb_str_pad($str, $pad_length, $pad_string, $pad_type);
echo $result; // Hello
예제 4: 특정 문자열 추가
#hostingforum.kr
php
$str = "Hello";
$pad_length = 10;
$pad_string = "*";
$result = mb_str_pad($str, $pad_length, $pad_string);
echo $result; // Hello*
예제 5: 한글 문자열 추가
#hostingforum.kr
php
$str = "안녕하세요";
$pad_length = 10;
$pad_string = "*";
$result = mb_str_pad($str, $pad_length, $pad_string);
echo $result; // 안녕하세요*
이러한 예제를 통해 `mb_str_pad` 함수의 사용법을 이해할 수 있습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.