Home  >  Article  >  Backend Development  >  How to remove the last few digits of a string in php

How to remove the last few digits of a string in php

藏色散人
藏色散人Original
2021-07-10 09:55:122897browse

In PHP, you can use the substr function to remove the last few digits of a string. The implementation code statement is such as "echo substr("Hello world",0,5)".

How to remove the last few digits of a string in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to remove the last few digits of a string in php ?

In PHP, you can use the substr function to remove the last few digits of a string.

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

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

Example:

Use start and length parameters with different positive and negative numbers:

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

Running results:

Hello worl
ello wor
Hello
world
Hello worl
ello wor
Hello
world

Recommended learning: "PHP video tutorial

The above is the detailed content of How to remove the last few digits of a string 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