PHP substr() function string interception usage example explanation

怪我咯
Release: 2023-03-13 15:26:01
Original
2119 people have browsed it

substr() Function Returns part of string. Returns the extracted part of the string, returns FALSE if it fails, or returns an empty string.

Note: If the start parameter is a negative number and length is less than or equal to start, then length is 0.

The substr function in PHP is defined as follows:

substr(string,start,length)
Copy after login

The parameter description is as follows:

ParameterDescription
stringRequired. Specifies a part of the string to be returned.
start

Required. Specifies where in the string to begin.

  • Positive numbers - start at the specified position in the string

  • Negative numbers - start at the specified position from the end of the string

  • 0 - Starts at the first character in the string

length

Optional. Specifies the length of the returned string. The default is until the end of the string.

  • Positive number - the length returned from the position of the start parameter

  • Negative number - the length returned from the end of the string Length

##The sample code is as follows:

<?php
 echo substr("Welcome to www.jb51.net!",0); //原样输出,不截取
 echo "<br>";
 echo substr("Welcome to www.jb51.net!",4,14); //从第4个字符开始连续截取14个字符
 echo "<br>";
 echo substr("Welcome to www.jb51.net!",-4,4); //从倒数第4个开始截取4个字符
 echo "<br>";
 echo substr("Welcome to www.jb51.net!",0,-4); //从第一个字符开始截取,截取到倒数第4个字符
?>
Copy after login

The running result is as follows:

Welcome to www.jb51.net!
ome to www.jb5
net!
Welcome to www.jb51.
Copy after login

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

Related labels:
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!