Home >Backend Development >PHP Problem >Usage of php substring

Usage of php substring

藏色散人
藏色散人Original
2020-08-20 10:49:223101browse

There is no substring function directly available in php, but there is the substr function. The substr function is used to return a part of the string, and its syntax is "substr(string,start,length)".

Usage of php substring

Recommended: "PHP Video Tutorial"

Talk about the correct usage of substr and substring in PHP And introduction to related parameters

Everyone knows that there are functions substr and substring for string interception characters in js. What about PHP? PHP does not have a directly available substring function, but there is a substr function.

If you don’t believe it, you can test it yourself.

The correct code is given below.

<?
$a="me";
echo(substr($a,,));//输出me
?>

Another incorrect code is given below

<?
$a="me";
echo(subString($a,,));
?>

substr() function returns part of the string.

substr(string,start,length)

string: The string to be intercepted

start:

Positive number - starts at the specified position of the string

Negative number - starts from the string The end starts at the specified position

0 - starts at the first character in the string

length:

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

Positive number - returns from the position of the start parameter

Negative number - returns from the end of the string

Detailed explanation of the usage of PHP substr()

Definition and Usage

substr() function returns a part of a string. Using the substr() function to intercept Chinese may cause garbled characters. It is recommended to use the mb_substr() function to intercept Chinese.

Syntax

substr(string,start,length)

Parameters

string Required. Specifies a part of the string to be returned.

start

Required. Specifies where in the string to begin.

Positive number - starts at the specified position in the string

Negative number - starts at the specified position from the end of the string

0 - at the first character in the string Start at

length

optional. Specifies the length of the string to be returned. The default is until the end of the string.

Positive numbers - Returns from the position of the start parameter

Negative numbers - Returns from the end of the string

Tips and comments

Comments: If start is a negative number And length is less than or equal to start, then length is 0.

Example

<?php
$str = &#39;hello world!&#39;;
echo substr($str, 4); // o world! 左起第4开始向右截取到末尾
echo substr($str, 4, 5); // o wor 左起第4开始向右取5位
echo substr($str, 4, -1); // o world 左起第4与右起第1之间的字符
echo substr($str, -8, 4); // o wo 右起第8开始向右截取4位
echo substr($str, -8,-2); // o worl 右起第8与右起第2之间的字符
?>

The above is the detailed content of Usage of php substring. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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