Home >Backend Development >PHP Tutorial >PHP intercepts a string and pads it with zeros str_pad() function_PHP tutorial

PHP intercepts a string and pads it with zeros str_pad() function_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:29:351154browse

Definition and Usage
str_pad() function pads a string to the specified length.

Syntax
str_pad(string,length,pad_string,pad_type) Parameter Description
string Required. Specifies the string to be filled.
length required. Specifies the length of the new string. If the value is less than the length of the original string, no operation is performed.
pad_string optional. Specifies the string used for padding. The default is blank.
pad_type optional. Specifies the side of the padding string.

Possible values:

STR_PAD_BOTH - Pad to both ends of the string. If it's not an even number, the right side gets extra padding.
STR_PAD_LEFT - Pad to the left side of the string.
STR_PAD_RIGHT - Pad to the right side of the string. This is the default.


Example 1
$str = "http://www.jb51.net";
echo str_pad($str,30,"." );
?>

Output:

http://www.jb51.net.....

Example 2
$str = "http://www.jb51.net";
echo str_pad($str,30,".",STR_PAD_LEFT);
?>

Output:

.....http://www.jb51.net

Example 3
$str = "http ://www.jb51.net";
echo str_pad($str,30,".:",STR_PAD_BOTH);
?>

Output:

. :.:http://www.jb51.net.:.:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323374.htmlTechArticleDefinition and usage The str_pad() function fills a string to the specified length. Syntax str_pad(string,length,pad_string,pad_type) Parameter Description string Required. Specifies the string to be filled. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn