Home  >  Article  >  Backend Development  >  How to add 0 in front of the string if the string length is not long enough in php

How to add 0 in front of the string if the string length is not long enough in php

青灯夜游
青灯夜游Original
2022-08-16 20:16:202995browse

In PHP, you can use the str_pad() function to pad 0 if the string is not enough. This function can use specified characters to fill the string to the specified length. You only need to set the third parameter to "0" and the fourth parameter to "STR_PAD_LEFT" to realize that if the string length is not enough, fill it with 0 in front. Syntax "Syntax "str_pad(string, specified length, "0", STR_PAD_LEFT)".

How to add 0 in front of the string if the string length is not long enough in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1 , DELL G3 computer

In php, you can use the str_pad() function to add 0 in front if the string length is not long enough.

str_pad() function can Fill the string to the new length, that is, fill the string to the specified length with the specified characters.

str_pad(string,length,pad_string,pad_type)
Parameter Description
string Required. Specifies the string to be padded.
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. Default is blank.
pad_type Optional. Specifies the padding string Which side of the string.

Possible values:

  • STR_PAD_BOTH - padding on both sides of the string. If not even, the right side gets extra padding.
  • STR_PAD_LEFT - padding The left side of the string.
  • STR_PAD_RIGHT - Pads the right side of the string. This is the default.
  • ##Return value: Returns the filled string.


#If you want to use the str_pad() function to realize that the string length is not long enough to add 0 in front, you only need the third one Set the parameter pad_string to "0", and set the fourth parameter to "STR_PAD_LEFT".

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="123456";
echo "原字符串:".$str."<br><br>";
echo "前面用0补位后:".str_pad($str,10,"0",STR_PAD_LEFT)."<br><br>";
?>

How to add 0 in front of the string if the string length is not long enough in php

Let's take a look at the other two values ​​​​of the fourth parameter. :

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="123456";
echo "原字符串:".$str."<br><br>";

echo "后面用0补位后:".str_pad($str,10,"0")."<br><br>";
echo "首尾用0补位后:".str_pad($str,10,"0",STR_PAD_BOTH)."<br><br>";
?>

How to add 0 in front of the string if the string length is not long enough in php

Recommended study: "

PHP Video Tutorial"

The above is the detailed content of How to add 0 in front of the string if the string length is not long enough in php. For more information, please follow other related articles on the PHP Chinese website!

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