search
HomeBackend DevelopmentPHP ProblemHow to remove the last character of a string in php

Two removal methods: 1. Use substr() to remove. You only need to set the second parameter of the function to 0 and the third parameter to (string length-1). Syntax "substr($str,0,strlen($str)-1)". 2. Use str_split() to convert the string into a character array, use array_pop() to delete the last element in the character array, and then use implode() to convert the character array back to a string.

How to remove the last character of a string in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php removes strings Two methods for the last character

Method 1: Use the substr() function to remove

The substr() function can remove the character from the string. Intercept characters of a certain length at the specified position.

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.
  • Positive number - starts at the specified position in the string
  • Negative number - starts at the specified position from the end of the string
  • 0 - at the first character in the string Start at
length Optional. Specifies the length of the string to be returned. The default is until the end of the string.
  • Positive number - Returns from the position of the start parameter
  • Negative number - Returns from the end of the string

Just set the second parameter of the function to 0 and the third parameter to (string length-1).

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="Hello world";
echo "原字符串:".$str."<br>";
echo "去除最后一位字符:".substr($str,0,strlen($str)-1)."<br>";
?>

How to remove the last character of a string in php

Method 2: Use str_split() array_pop() implode() function

  • Use str_split () function splits the string into an array, that is, converts the string into a character array.

  • Use the array_pop() function to delete the last character element in the character array

  • Use the implode() function to convert the character array back to a string

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="Hello!";
echo "原字符串:".$str."<br>";
$arr=str_split($str);
array_pop($arr);
echo "去除最后一位字符:".implode($arr)."<br>";
?>

How to remove the last character of a string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove the last character 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
php怎么将数值后面的零去掉php怎么将数值后面的零去掉May 30, 2022 pm 07:36 PM

两种去除方法:1、使用rtrim()函数,语法“rtrim(数值,"0")”,可去除数值后面连续的零。2、用substr_replace(),语法“substr_replace(数值,'', -位置值)”,可从指定位置开始将零替换为空字符。

php怎么将空格转换成nbsp符php怎么将空格转换成nbsp符Apr 25, 2022 pm 08:30 PM

方法:1、用str_replace(),语法“str_replace(" ","&nbsp;",$str)”;2、用preg_replace()配合正则表达式,语法“preg_replace('/\s+/',"&nbsp;",$str)”。

php怎么将小数点转为百分比php怎么将小数点转为百分比Apr 28, 2022 pm 08:51 PM

php将小数点转为百分比的方法:1、利用“*”运算符将小数乘以100,语法“小数 * 100”,可将小数转为百分值;2、使用字符串拼接符“.”将百分值和“%”符拼接在一起,形成百分比字符串即可,语法“百分值."%"”。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

怎么将多个php数组转成一个json数据怎么将多个php数组转成一个json数据May 26, 2022 pm 04:18 PM

转换方法:1、使用“array_merge_recursive(数组1,数组2,数组3...)”语句将多个数组合并为一个数组;2、使用json_encode()将合并后的数组转为json数据,语法“json_encode(合并后的数组)”。

php怎么去除字符串中某几个子字符串php怎么去除字符串中某几个子字符串Apr 26, 2022 pm 09:03 PM

在php中,可以利用str_replace()函数来去除字符串中的多个指定子字符串,只需要将该函数的第一个参数设为包含多个值的数组,第二个参数设为空字符即可;语法“str_replace(数组,'',$str)”,会将多个子串替换为空字符。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.