How to use php str_pad function?

青灯夜游
Release: 2023-04-06 18:48:02
Original
2690 people have browsed it

php The str_pad() function is used to fill a string to a specified length. The syntax is "str_pad(string,length,pad_string,pad_type)" and returns the filled string; it can be filled with any other string Enter a string until it reaches the specified length.

How to use php str_pad function?

#php How to use str_pad function?

php str_pad function can pad the string to the specified length

Syntax:

str_pad(string,length,pad_string,pad_type)
Copy after login

Parameters:

● String is required, specifies the string to be filled,

● length is required, specifies the new string length, if the value is less than the original length of the string, then No action is taken.

● pad_string is optional and specifies the string used for padding. The default is blank.

● pad_type is optional and specifies which side of the string to fill. STR_PAD_BOTH -- fills both sides of the string. If it is not an even number, the right side gets extra padding. STR_PAD_LEFT – pad the left side of the string. STR_PAD_RIGHT -- pad the right side of the string, default.

Description: We can fill the input string with any other string until the specified length is reached. If we do not pass another string to the str_pad() function, then the input string will be padded with spaces.

Example:

<?php
$i = "hello world";
$j = str_pad($i,30,"!"); //未设置填充字符串位置,默认为右侧
echo $j;
echo "<br>";
$k = "hello world";
$h = str_pad($k,30,".",STR_PAD_LEFT);//设置填充字符串的位置为左侧
echo $h;
?>
Copy after login

Output:

hello world!!!!!!!!!!!!!!!!!!!
...................hello world
Copy after login

The above is the detailed content of How to use php str_pad function?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template