Detailed explanation of usage of PHP interception string function substr() function example

伊谢尔伦
Release: 2023-03-07 13:00:02
Original
9443 people have browsed it

There is a very important technology in PHP, which is to intercept characters of a specified length in a specified string. PHP can use the PHP predefined function substr() function to implement string interception. Let's introduce the syntax and application of the substr() function.

substr() function syntax format is as follows:

substr(string, start, length);
Copy after login

substr() function parameter description is as follows:

Parameter Number Description
stringSpecifies the string Object
start specifies the position where to start intercepting the string. If the parameter start is a negative number, it will be intercepted from the end of the string.
#length Optional parameter specifies the number of characters to intercept. If length is a negative number, then Indicates that the penultimate length character

is obtained. Note: The specified position of

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

start reads characters from left to right when the data is positive

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

length The default is until the end of the string, positive Number - starts at the specified position in the string, negative number - 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. The example code is as follows:

<?php
  echo substr("abcdefg",0);              //从第0个字符开始截取
  echo "<br>";
  echo substr("abcdefg",2,4);             //从第2个字符开始连续截取4个字符
  echo "<br>";
  echo substr("abcdefg",-4,2);             //从倒数第4个字符开始截取2个字符
  echo "<br>";
  echo substr("abcdefg",0,-4);             //从第0个字符开始截取,截取到倒数第4个字符
?>
Copy after login

The output result is:

abcdefg
cdef
de
abc
Copy after login

When developing some web programs, In order to maintain a reasonable layout of the entire page, it is often necessary to display only part of the information in some extremely long texts. Let's explain how to implement it through specific examples.

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

<?php
   $str="沪深证券交易所已发布“高送转”信息披露指引,实施“刨根问底”问询,开展对“高送转”内幕交易核查联动,集中查办了一批借“高送转”之名从事内幕交易或信息披露违规案件。";
  if(strlen($str)>30){                    //如果文本的字符串长度大于30
      echo substr($str,0,30). "....";          //输出文本的前30个字节,然后输出省略号
      }else{                            
        echo $str;                   // 如果文本的字符串长度小于30,直接输出原文本
 }
?>
Copy after login

The output result is:

沪深证券交易所已发布....
Copy after login

Description: Truncate characters of a certain length from the specified string at the specified position. The substr() function can be used to obtain a 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 the intercepted Chinese string to be garbled, so substr( ) function is suitable for intercepting English strings. If you want to intercept Chinese strings and avoid garbled characters, the best way is to write a custom function using the substr() function.

The above is the detailed content of Detailed explanation of usage of PHP interception string function substr() function example. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!