PHP has many methods and functions for processing strings, which can be used to remove the last character of a string. In some cases, web designers will want the last character of a string to disappear, such as displaying news topics A list or a string of multiple sections. Maybe the last character is a special symbol or punctuation mark, but you don’t want the last character to appear on the web page. You can do this through PHP functions. This article For example, the two methods have completely different processing concepts, but both can handle the effects we want.
Example 1: Use the substr and strlen functions to remove the last character<?php
$TestString="ABCDEF";
echo substr($TestString,0,-1);
?>
The above example outputABCDE
Because the string length of English characters and traditional Chinese characters is different, a text in Traditional Chinese is twice as long as an English word, so in the example, use the substr function to directly set the initial parameter to 0 and the length parameter Set to -1, which means to extract the entire string, and then remove the last string, there will be no last character when outputting. For the parameter usage of PHP's substr function, please refer to " php substr() The function gets part of the string, and the length of the string can be set ."Although it is convenient to use the substr function, if it is used in Traditional Chinese, incomplete string removal may occur. The solution is to rewrite the second line of the example to "echo substr($TestString ,0,strlen('字')*-1);” In this way, the "character" in the parentheses can be changed to other traditional Chinese characters or symbols, and the length of the string is calculated through the strlen function, which is used with the substr function To create the effect of removing a traditional Chinese character element from the suffix, then we can look at the replacement technique using the substr_replace function, and this effect can be made quickly.Example 2: Replace the last character with substr_replace<?php
$TestString="ABCDEF";
echo substr_replace($TestString,'',-1);
?>
The above example outputABCDE
Different from the principle of calculating the string and removing the fixed-length string from the end in Example 1, the substr_replace function starts from a certain place in the string and replaces the original with the set character according to the set string length The character of, in the way of writing in the example, it means to count a character from the back to the front, and replace it with a null value, and naturally remove the last character. This way Processing English strings is very concise and fast, but processing Chinese strings will also face the same problems as Example 1. Therefore, when processing traditional Chinese characters, the second line in the example can be rewritten as "echo substr_replace($TestString,", strlen('字')*-1);” In this way, the strlen function is also used to calculate the length of the text first, and then start the replacement action, and finally get the same result. For the parameter usage of the substr_replace function, please refer to: PHP substr_replace (editing).
Post a Comment
0 Comments