PHP Intercept string function substr () function instance usage detailed

Source: Internet
Author: User
A very important technique in PHP is to intercept characters of a specified length in a specified string. PHP pre-defined functions for string interception can be used substr ()function to implement. The following is an introduction to the syntax of the substr () function and its application.

The syntax format for the substr () function is as follows:

SUBSTR (String, start, length);

The substr () function parameters are described below:

Parameters Description
String Specifying a String Object
Start Specifies the position at which to begin intercepting the string. If the argument start is negative, The intercept starts at the end of the string
Length An optional parameter that specifies the number of characters to intercept, or if the length is negative, to take the length of the penultimate character

Description

The specified position of start is calculated from 0, where the position of the first character in the string is represented as 0.

Start is positive data reads characters from left to right

Right-to-left reading of characters when start is negative

Length is the default until the end of the string, positive-starts at the specified position in the string, and negative-starts at the specified position from the end of the string.

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

<?php  Echo substr ("ABCDEFG", 0);              Capture  Echo "<br>" from the No. 0 character;  echo substr ("ABCDEFG", 2,4);             4 Characters of  echo "<br>" starting from the 2nd character;  echo substr ("ABCDEFG", -4,2);             The 2-character  echo "<br>" starts from the 4th character in the countdown.  echo substr ("ABCDEFG", 0,-4);             Intercept from the No. 0 character and intercept to the 4th character?>

The output is:

Abcdefgcdefdeabc

In the development of some web programs, in order to maintain a reasonable layout of the entire page, often need for some extra-long text, only required to display some of the information. The following is a concrete example of how it is implemented.

Use the substr () function to intercept partial string information for extra-long text, with the remainder using "..." instead, the sample code is as follows:

<?php   $str = "The Shanghai and Shenzhen Stock Exchange has issued a" high transfer "information disclosure guidelines, the implementation of" inquiry "inquiries, the" high transfer "insider trading verification linkage, focused on a group of" high transfer "in the name of engaging in insider trading or information disclosure violation cases. ";  if (strlen ($STR) >30) {                    //if the string length of the text is greater than the      echo substr ($str, 0,30). "....";          Output the first 30 bytes of text, and then output the ellipsis      }else{                                    echo $str;                   If the string length of the text is less than 30, direct output of the original text}?>

The output is:

SHANGHAI-Shenzhen Stock Exchange has been released ....

Description: intercepts a certain length of character from the specified string in the specified position. You can get a portion of a fixed format string by using the substr () function.

Note: using the substr () function when intercepting a string, if the number of truncated strings is odd, it will result in the interception of Chinese characters garbled, so the substr () function for the interception of English strings, if you want to intercept the Chinese string, And to avoid garbled, the best way is to use the substr () function to write a custom function.

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.