Several methods of left complement 0 string filling and auto completion of PHP numeric string

  • 2021-06-28 08:53:10
  • OfStack

1. Number complement 0.


If you want to automatically generate a number, automatically generate a number, such as the form "d0000009", "d0000027", then you will face a problem, how can you complete the left side with 0 to such an 8-digit code?I thought about two ways to do this.

Method 1:

First construct a number of 10,000,000,000,000, that is, 1,7 zeros, then add the current number (for example, 3), then you get 10,000,3, intercept substr ('10000003', 1,7) with the string, then you get 0000003, and finally stitch with "d" to get the final number d0003.

The source code is as follows:


<?php
 $num = 3;
 $temp_num = 10000000;
 $new_num = $num + $temp_num;
 $real_num = "d".substr($new_num,1,7); // That is, intercept the top " 1 " 
 echo $real_num;
?>

Method 2:

Measure the length of the current number (for example, 3), strlen ('3') =1. Subtract the length of the current number from the total length of the number to be generated to get the number of zeros that need to be filled, and then fill 0 with the for loop.

The source code is as follows:


<?php
 $num = 3;
 $bit = 7;// produce 7 Number of digits 
 $num_len = strlen($num);
 $zero = '';
 for($i=$num_len; $i<$bit; $i++){
  $zero .= "0";
 }
 $real_num = "d".$zero.$num;
 echo $real_num;
?>


Method 3: Several other methods


<?php
    $sourceNumber = "1";
    $newNumber = substr(strval($sourceNumber+1000),1,3);
    echo "$newNumber";
?>
/* This will happen: 001
 If you want to increase the number of digits, you can 1000 Increase, and then turn the 3 It also increases. 
 Example: If I want to add  "4 individual 0"  No. 03 That's ok   That's about to happen. */


<?php
    $newNumber = substr(strval($sourceNumber+100000),1,5);
?>
/* In fact, if you want to display a few digits in total, you can $sourceNumber+1 How many to add after 0 And finally 1 The number of digits changes directly to how many digits are displayed. */


/*string str_pad ( string $input, int $pad_length [, string $pad_string [, int $pad_type]] )*/
<?php 
$input = "Alien"; 
echo str_pad($input, 10); 
// produces "Alien " 
echo str_pad($input, 10, "-=", STR_PAD_LEFT); 
// produces "-=-=-Alien" 
echo str_pad($input, 10, "_", STR_PAD_BOTH); 
// produces "__Alien___" 
echo str_pad($input, 6 , "___"); 
// produces "Alien_" 
?>
/* Complete string length . with pad_string  repair . The default complement is on the right if STR_PAD_LEFT To the left, STR_PAD_BOTH Both sides 1 Start and supplement.Next time use str_pad After all, it's built-in and definitely faster than customizing. */


/*
 I don't think the above method is very good, introduction 1 Write by me 1 Methods.The method function is as follows, so when you want the result 001 If so, how to: dispRepair('1',3,'0')
 Function: complement function 
str: Original string 
type : Type, 0 To supplement, 1 Pre-supplement 
len : New string length 
msg : padding character 
*/
function dispRepair($str,$len,$msg,$type='1') {
  $length = $len - strlen($str);
  if($length<1)return $str;
  if ($type == 1) {
    $str = str_repeat($msg,$length).$str;
  } else {
    $str .= str_repeat($msg,$length);
  }
  return $str;
}


2. String Filling, Auto-Completion, Auto-Completion

When you encounter the output of a fixed length string, you can use two methods under 1 to auto-fill and auto-complete the PHP string.

Method 1:

$newStr= sprintf('%05s', $str);

The function of sprintf() is very flexible. In the format string above,'%05s'means that the output length is 5 strings. If the length is insufficient, complete the left with zero;If it is written as'%5s', it is filled with spaces by default;If you want to use other character completion, preface the character with a single quotation mark, i.e., the expression'%'#5s'is completed with a pound sign.Finally, if you want completion to occur to the right of the string, add a minus sign after the percent sign, "%-05s".

Method 2:

[code]$cd_no = str_pad(++$next_cd_no,8,'#',STR_PAD_LEFT);

str_pad (string, length, pad_string, pad_type: Check the manual for specific usage.

string is required.Specifies the string to be populated.
length is required.Specifies the length of the new string.If the value is less than the length of the original string, no action is taken.
pad_string is optional.Specifies the string to be used for padding.The default is blank.
pad_type is optional.Specify the side that fills the string.

These two methods are very convenient for the automatic completion of PHP strings.


Related articles: