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

What is the usage of substr() function in php

王林
Release: 2023-03-04 20:56:01
Original
4140 people have browsed it

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)
Copy after login

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);
?>
Copy after login

Output:

world!
Copy after login

Example 2:

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

Output:

world
Copy after login

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!

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