Home  >  Article  >  Backend Development  >  How to delete the last character in php

How to delete the last character in php

藏色散人
藏色散人Original
2021-06-01 09:56:125451browse

php method to delete the last character: 1. Use "substr($arr_str,0,strlen($arr_str)-1);" to delete; 2. Use "substr($arr_str, 0, -1) ) method to delete; 3. Delete through the "rtrim" method.

How to delete the last character in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

Three ways to delete the last character of a string in PHP programs

Common syntax format:

foreach ($arr as $key => $value) {
$arr_str = $arr['x_id'] . ',' . $arr_str;
}

Assume that the characters in the character array $arr are

arr[0] = 'a';
arr[1] = 'b';
arr[2] = 'c';

Then, the spliced ​​$arr_str string is a, b, c. At this time, we need to delete the last character ','.

Delete in two php Summary of the method for the last character:

Method 1:

substr($arr_str,0,strlen($arr_str)-1);

Detailed explanation: substr() function syntax: string substr ( string $string , int $start [, int $length ] )

Strlen () Function: Int Strlen (String $ String)

This Example Principle:

## First use the strlen () function to determine the length of the character string $ arr_str, and then use it to use The substr() function intercepts $arr_str to the penultimate digit of $arr_str. This removes the last ",".

Usage experience:

Not recommended, php There is a simpler and more useful way!

Method 2:

substr($arr_str, 0, -1)

Detailed explanation: directly use the substr() function to cut off the last character in reverse order;

Use Feeling: It’s still very appropriate~~ However, first you have to make sure that there must be content in the string, and the last digit must be missing!

Method 3:          

rtrim($arr_str, ",")

Detailed explanation: rtrim() function Syntax: string rtrim ( string $str [, string $character_mask ] )

rtrim - delete the blank characters (or other characters) at the end of the string

//系统常量定义 
//去THinphp手册中进行查找 
echo "<br>"."网站的根目录地址".__ROOT__." "; 
echo "<br>"."入口文件地址".__APP__." "; 
echo "<br>"."当前模块地址".__URL__." "; 
echo "<br>"."当前url地址".__SELF__." "; 
echo "<br>"."当前操作地址".__ACTION__." "; 
echo "<br>"."当前模块的模板目录".__CURRENT__." "; 
echo "<br>"."当前操作名称".ACTION_NAME." "; 
echo "<br>"."当前项目目录".APP_PATH." "; 
echo "<br>"."当前项目名称".APP_NAME." "; 
echo "<br>"."当前项目的模板目录".APP_TMPL_PATH." "; 
echo "<br>"."项目的公共文件目录".APP_PUBLIC_PATH." "; 
echo "<br>"."项目的配置文件目录".CONFIG_PATH." "; 
echo "<br>"."项目的公共文件目录".COMMON_PATH." "; 
//自动缓存与表相关的全部信息 
echo "<br>"."项目的数据文件目录".DATA_PATH." runtime下的data目录"; 
echo "<br>"." ".GROUP_NAME.""; 
echo "<br>"." ".IS_CGI.""; 
echo "<br>"." ".IS_WIN.""; 
echo "<br>"." ".LANG_SET.""; 
echo "<br>"." ".LOG_PATH.""; 
echo "<br>"." ".LANG_PATH.""; 
echo "<br>"." ".TMPL_PATH.""; 
//js放入的位置,供多个应用的公共资源 
echo "<br>"." ".WEB_PUBLIC_PATH."";

Recommended learning: "

PHP Video tutorial

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