Home >Backend Development >PHP Problem >How to remove the last three digits from a string in php
Two elimination methods: 1. Using the substr() function, you only need to set the second parameter to 0 and the third parameter to "-3". The syntax is "substr($str, 0,-3)”. 2. To use the substr_replace() function, you only need to set the second parameter to an empty string and the third parameter to "-3". The syntax is "substr_replace($str,"",-3)".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php removes strings Method for the last three digits
Method 1: Use the substr() function to remove
The substr() function returns part of the string.
substr(string,start,length)
Parameters | Description |
---|---|
string | Required . Specifies a part of the string to be returned. |
start | Required. Specifies where in the string to begin.
|
length | Optional. Specifies the length of the string to be returned. The default is until the end of the string.
|
Just set the second parameter of the function to 0 and the third parameter to a negative value (-3).
<?php header('content-type:text/html;charset=utf-8'); $str = "hello123"; echo "原字符串:".$str."<br>"; echo "删除后3个字符:".substr($str,0,-3); ?>
Method 2: Use the substr_replace() function to eliminate
The substr_replace() function can replace the specified position starting from the specified position in the string number of characters.
substr_replace(string,replacement,start,length)
Parameters | Description |
---|---|
string | Required . Specifies the string to check. |
replacement | Required. Specifies the string to be inserted. |
start | Required. Specifies where in the string to begin replacement.
|
length | Optional. Specifies how many characters to replace. The default is the same as the string length.
|
substr_replace() Replaces the substring qualified by the start and optional length parameters in a copy of string string using replacement.
If start is a positive number, replacement will start from the start position of string. If start is negative, the replacement will start at the start position from the bottom of string.
If the length parameter is set and is a positive number, it represents the length of the replaced substring in string. If set to a negative number, it represents the number of characters from the end of the substring to be replaced from the end of string. If this parameter is not provided, the default is strlen(string) (the length of the string). Of course, if length is 0, then the function of this function is to insert replacement at the start position of string.
You only need to set the second parameter of the function to an empty string, and set the third parameter to a negative value (-N). It will be the Nth from the bottom of the string. Starting with characters, replace all remaining characters with an empty string, that is, remove the last N characters of the string.
If you want to remove the last 3 digits, set N to 3.
<?php header('content-type:text/html;charset=utf-8'); $str = 'hello world!'; echo "原字符串:".$str."<br><br>"; $replace = ''; echo "替换后3位字符:".substr_replace($str, $replace,-3); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove the last three digits from a string in php. For more information, please follow other related articles on the PHP Chinese website!