PHP string completion, PHP string auto-filling, PHP output fixed length

Source: Internet
Author: User

From: http://blog.163.com/dingjie_china/blog/static/1228662992011626115229455/

You can use either of the following methods to output a string of a certain length.PHP string auto-filling and auto-completion.

Method 1:

$ Newstr = sprintf ('% 05s', $ Str );

Sprintf () is very flexible. In the preceding format string,% 05 s"Indicates the output is a string of 5 characters. If the length is insufficient, the left side is supplemented with zero. If it is written as"% 5sBy default, space is used for completion. If you want to use other characters for completion, you must add a single quotation mark before the character, that is, in the form of"% '#5 S"Indicates completion by the well number. Finally, If you want completion to happen on the right side of the string, add the minus sign after the percent sign."%-05 s".

Method 2:

$ Cd_no = str_pad (++ $ next_cd_no, 8, '#', str_pad_left );

Str_pad (string, length, pad_string, pad_type): For detailed usage, see the manual.

String is required. Specifies the string to be filled.
Length is required. Specify the length of the new string. If the value is smaller than the length of the original string, no operation is performed.
Pad_string is optional. The specified string for filling. The default value is blank.
Pad_type is optional. Specify the other side of the string to be filled.

These two methods are easy to implement.PHP string auto-completionFunction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.