Home > Backend Development > PHP Problem > How to remove the first digit in php

How to remove the first digit in php

青灯夜游
Release: 2023-03-15 18:44:01
Original
2904 people have browsed it

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)".

How to remove the first digit in php

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

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$num=123456789;
echo substr_replace($num,"",0,1);
?>
Copy after login

How to remove the first digit in php

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

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$num=123456789;
echo substr($num,1);
?>
Copy after login

How to remove the first digit in php

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!

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