Home >Backend Development >PHP Problem >How to remove the first digit in php
Removal method: 1. Use substr_replace() function to replace the first digit with an empty string, the syntax is "substr_replace($num,"",0,1)"; 2. Use substr to intercept the first digit from All characters starting with two digits are enough, the syntax is "substr($num,1)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, I will give you a large list of values , for example "123456789", how to remove the first digit "1", that is, return "23456789"?
In fact, PHP provides a variety of solutions. The following article will introduce two methods to you.
Method 1: Use the substr_replace() function
The substr_replace() function replaces part of a string with another string.
substr_replace(string,replacement,start,length)
When the replacement value replacement
parameter is set to the empty string ''
, the function of deleting the substring can be realized.
If you want to remove the first numeric character, you need to set the replacement position start to 0 and the replacement number lengths to 1.
Implementation example:
<?php header('content-type:text/html;charset=utf-8'); $num=123456789; echo substr_replace($num,"",0,1); ?>
Method 2: Use substr function
substr() function Characters of a certain length can be intercepted from a specified position in a string.
substr(string, start , length)
When the interception position start is set to 1 and the interception length length is omitted, all remaining characters can be intercepted starting from string index 1 (the second character), and the first character can be deleted. Numeric characters.
Implementation example:
<?php header('content-type:text/html;charset=utf-8'); $num=123456789; echo substr($num,1); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove the first digit in php. For more information, please follow other related articles on the PHP Chinese website!