Home > Backend Development > PHP Tutorial > php string filling function

php string filling function

WBOY
Release: 2016-07-29 09:13:15
Original
2060 people have browsed it

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>)
Copy after login

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);
?>
Copy after login
Example 2:

Padding both sides of the string:

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

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.

Related labels:
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