Home >Backend Development >PHP Problem >What is the usage of substr() function in php

What is the usage of substr() function in php

王林
王林Original
2020-08-20 13:37:504174browse

Usage of substr() function in php: The substr() function is used to return a part of a string. Function syntax: [substr(string, start, length)], where the parameter length is optional and is used to specify the length of the string to be returned.

What is the usage of substr() function in php

#The substr() function is used to return a part of a string.

(Recommended tutorial: php graphic tutorial)

Function syntax:

substr(string,start,length)

Parameter introduction:

  • string Required. 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 - Start at the specified position in the string Starts at the first character in

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

Positive number - Return from the position of the start parameter

Negative number - Return from the end of the string

(Learning video recommendation: php Video Tutorial )

Code Example:

Example 1:

<?php
echo substr("Hello world!",6);
?>

Output:

world!

Example 2:

<?php
echo substr("Hello world!",6,5);
?>

Output:

world

The above is the detailed content of What is the usage of substr() function in php. 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