Home > Backend Development > PHP Problem > How to use substr function in PHP?

How to use substr function in PHP?

Guanhui
Release: 2023-03-03 15:58:01
Original
4630 people have browsed it

The function of the substr function in PHP is to return a part of the string. Its syntax is "substr(string,start,length)". Its parameter string indicates that part of the string is returned, and the parameter start indicates that it is in the string. Where to start, the parameter length indicates the interception length.

How to use substr function in PHP?

Usage examples

<?php
echo substr("Hello world",10)."<br>";
echo substr("Hello world",1)."<br>";
echo substr("Hello world",3)."<br>";
echo substr("Hello world",7)."<br>";

echo substr("Hello world",-1)."<br>";
echo substr("Hello world",-10)."<br>";
echo substr("Hello world",-8)."<br>";
echo substr("Hello world",-4)."<br>";
?>
Copy after login
<?php
echo substr("Hello world",0,10)."<br>";
echo substr("Hello world",1,8)."<br>";
echo substr("Hello world",0,5)."<br>";
echo substr("Hello world",6,6)."<br>";

echo substr("Hello world",0,-1)."<br>";
echo substr("Hello world",-10,-2)."<br>";
echo substr("Hello world",0,-6)."<br>";
echo substr("Hello world",-2-3)."<br>";
?>
Copy after login

Recommended tutorial: "PHP"

The above is the detailed content of How to use substr function in PHP?. For more information, please follow other related articles on the PHP Chinese website!

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