Home  >  Article  >  Backend Development  >  How to remove the last word in php

How to remove the last word in php

PHPz
PHPzOriginal
2023-03-31 10:37:26990browse

PHP is a very popular server-side scripting language that can be used to develop web applications and websites. In PHP, sometimes we need to operate on strings, such as removing certain characters from the string or intercepting part of the string. The topic of this article is how to remove the last word of a PHP string.

First of all, we can intercept the string through PHP's built-in function substr. This function can return the truncated substring by passing in a string and two integer parameters. Among them, the first parameter is the string to be intercepted, the second parameter is the starting position of interception, and the third parameter is the length to be intercepted. Therefore, we can intercept the substring except the last character by calculating the length of the string and then passing in the starting position and the length of the string minus 1. The code example is as follows:

$str = "Hello World";
$str = substr($str, 0, strlen($str) - 1);
echo $str; // 输出结果为:Hello Worl

Another method is to use PHP's built-in function mb_substr. This function is similar to substr, but it is used to handle strings with multi-byte character sets. If some characters in the string are composed of multiple bytes, then using substr interception will not handle these characters correctly. Therefore, if your string contains some multi-byte characters such as Chinese, Japanese or Korean, you should use mb_substr to intercept. The code example is as follows:

$str = "我爱PHP";
$str = mb_substr($str, 0, mb_strlen($str) - 1);
echo $str; // 输出结果为:我爱PH

In addition to the interception method, a simpler method is to use PHP's built-in function substr_replace. This function can replace part of the string. We can replace the last character of the string with an empty string to remove the last character. The code example is as follows:

$str = "Hello World";
$str = substr_replace($str, "", -1);
echo $str; // 输出结果为:Hello Worl

Finally, we need to note that strings are immutable in PHP, which means that the value of the string cannot be changed once it is created. Therefore, none of the above three methods will change the value of the original string, but return a new string. If you need to change the value of the original string, you can assign the returned new string to the original string variable.

To sum up, we can use substr, mb_substr or substr_replace to remove the last character of the PHP string. Which method you choose depends on whether the string contains multibyte characters, as well as personal preferences and habits.

The above is the detailed content of How to remove the last word 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