Usage of the PHP string function substr () instance

Source: Internet
Author: User
One of the most important techniques in PHP is to intercept specified characters in a specified string. PHP can use the PHP pre-defined function substr () for string truncation. One of the most important techniques in PHP is to intercept specified characters in a specified string. PHP can use PHP predefined functions for string truncation. Substr ()Function. Next we will introduce the syntax of the substr () function and its application.

Substr () function syntax format:

substr(string, start, length);

The substr () function parameters are described as follows:

Parameter Description
String String object
Start Specifies the position where the string is intercepted. If the start parameter is a negative number, it is truncated from the end of the string.
Length Optional parameter. it specifies the number of characters to intercept. if the length is negative, it indicates that the last length character is obtained.

Note:

The specified start position starts from 0, that is, the position of the first character in the string is expressed as 0.

Start reads characters from left to right for positive data.

When start is a negative number, it reads characters from right to left.

Length ends until the end of the string by default. it is a positive value that starts at the specified position of the string, and a negative value that begins at the specified position at the end of the string.

Use the substr () function to intercept characters of the specified length in the string. the instance code is as follows:

 "; Echo substr (" abcdefg ", 2, 4); // captures 4 consecutive echo characters starting from 2nd characters"
"; Echo substr (" abcdefg ",-4th); // captures two echo characters starting from the last characters"
"; Echo substr (" abcdefg ", 0,-4); // It is truncated from 0th characters to the last 4th characters?>

Output result:

abcdefgcdefdeabc

When developing some web programs, in order to maintain a reasonable layout of the entire page, it is often necessary to only display part of the information for some ultra-long texts. The following describes the implementation methods through specific instances.

Use the substr () function to intercept part of the long text string information, and use the remaining part"....."The sample code is as follows:

 30) {// if the text string length is greater than 30 echo substr ($ str ). ".... "; // output the first 30 bytes of the text, and then output the ellipsis} else {echo $ str; // if the text string length is less than 30, directly output the original text}?>

Output result:

The Shanghai and Shenzhen stock exchanges have released ....

Note:Truncates a certain length of characters from a specified string based on the specified position. You can use the substr () function to obtain part of a fixed-format string.

Note:When using the substr () function to intercept a string, if the number of intercepted strings is an odd number, it will cause garbled characters in the intercepted Chinese strings, so substr () the function is suitable for intercepting English strings. if you want to intercept Chinese strings and avoid garbled characters, you can use the substr () function to compile a custom function.

The above is a detailed description of the usage of the PHP string function substr () function instance. For more information, see other related articles in the first PHP community!

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.