About pad Functions

Source: Internet
Author: User

Pad-"is the abbreviation of padding.

There are two functions in PHP -- at least two functions are available. I don't know if there are any other functions. The following details can be implemented: str_pad () and sprintf ().

Str_pad

As the name suggests, this function is for strings. This function can fill any other strings with the specified string.

Example: str_pad (string with padding, the length after filling, the string to fill, and the position to fill)

The length after filling must be a positive integer and there are three options to fill the position,

Left: str_pad_left,

Right: str_pad_right,

Two ends: str_pad_both

For example:

1 echo str_pad(1,8,”0″,STR_PAD_LEFT);

Result: 00000001

1 echo str_pad(1,8,”0″,STR_PAD_RIGHT);

Result: 10000000

1 echo str_pad(1,8,”0″,STR_PAD_BOTH);

Result: 00010000

In the above example, it is worth noting that if the number of digits to be filled is an odd number, for example, if the number of digits to be filled is 7 in the third case, the right side is given priority.

 

There is also a function like this in the array, which is array_pad. Use another str_pad method.

Basically the same

About pad Functions

Related Article

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.