Home > Backend Development > PHP Problem > 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
Release: 2023-03-15 09:08:01
Original
3096 people have browsed it

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

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

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!

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