Home  >  Article  >  Backend Development  >  php string filling function

php string filling function

WBOY
WBOYOriginal
2016-07-29 09:13:151990browse

Definition and usage

str_pad() function pads the string to the new length.

Syntax

str_pad(<span>string</span>,<span>length</span>,<span>pad_string</span>,<span>pad_type</span>)

Parameters Description
string Required. Specifies the string to be filled.
length Required. Specifies the new string length. If the value is less than the original length of the string, no operation is performed.
pad_string Optional. Specifies the string used for padding. The default is blank.
pad_type

Optional. Specifies which side of the string to fill.

Possible values:

  • STR_PAD_BOTH - Pad both sides of the string. If it's 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.
Example 1:

Padding the left side of the string:

<?php
$str = "Hello World";
echo str_pad($str,30,".",STR_PAD_LEFT);
?>
Example 2:

Padding both sides of the string:

<?php
$str = "Hello World";
echo str_pad($str,30,".:",STR_PAD_BOTH);
?>

The above introduces the PHP string filling function, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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