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

How to use substr function in PHP?

Guanhui
GuanhuiOriginal
2020-07-31 14:10:324708browse

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>";
?>
<?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>";
?>

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!

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