Home  >  Article  >  Backend Development  >  How to split a string in php and take the first few digits

How to split a string in php and take the first few digits

WBOY
WBOYOriginal
2022-02-10 17:01:113086browse

In PHP, you can use the substr() function to split a string and take the first few characters. This function can return a part of the specified string. You can use parameters to specify where to start splitting and what to cut. The length, the syntax is "substr(string, 0, first few digits)".

How to split a string in php and take the first few digits

The operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer

How to split a string in php and take the first few digits

The substr() function returns a part of a string.

Note: If the start parameter is negative and length is less than or equal to start, length is 0.

Syntax

substr(string,start,length)

The parameter settings are as shown in the following table:

How to split a string in php and take the first few digits

The example is as follows:

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

Output results:

How to split a string in php and take the first few digits

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to split a string in php and take the first few digits. 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