English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP String Character String Functions Manual
The str_pad() function is used to pad a string to a specified length using another string.
str_pad(string,length,pad_string,pad_type)
It is used to pad the string to a new length. The function returns the string after being padded from the left end, right end, or both ends to the specified length. If the optional pad_string parameter is not specified, the string will be filled with space characters, otherwise it will be filled with pad_string to the specified length.
It returns the padded string
Serial Number | Parameters and Description |
---|---|
1 | string Required. It specifies the string to be padded |
2 | length Required. It specifies the new string length |
3 | pad_string Optional. It specifies the string used for padding |
4 | pad_type Optional. Specify which side of the padding string.Possible Values:
|
Try the following example, using str_pad to pad strings:
<?php //Use: to pad the string on the right side $input = "w3 echo str_pad($input,18,":"); echo '<br>'; //Pad the string on the left side $str = "w3 echo str_pad($str,20,"!",STR_PAD_LEFT); echo '<br>'; //Pad the string on both sides $str = "wwww.w3 echo str_pad($str,20,"-",STR_PAD_BOTH); ?>Test and see‹/›
Output Result
w3codebox:::::::::::: !!!!!!!!!!!oldtoolbag.com ---wwww.oldtoolbag.com---