How do i get the last character of a string in php?

While working with PHP, it is often necessary to remove characters from strings.

In this snippet, we are going to show you several PHP functions that allow removing the last character from a particular string.

The first option is using the substr function. The syntax of this function is the following:

A simple example of the substr function is illustrated below:

$string = "Hello World!";
 echo "Given string: " . $string . "\n";
 echo "Updated string: " . substr($string, 0, -1) . "\n";

The output will look as follows:

Given string: Hello World!
Updated string: Hello World

The second option is using the substr_replace function. Its basic syntax is:

substr_replace($string ,"", -1);

And here is an example of using the substr_replace function:

$string = "Hello World!";
 echo "Given string: " . $string . "\n";
 echo "Updated string: " . substr_replace($string ,"",-1) . "\n";

And, here is the output:

Given string: Hello World!
Updated string: Hello World

This function also allows removing the last character of a string.

Here is the basic syntax:

In this syntax, you can notice “a” character, which should be removed.

So, an example of removing the character will look as follows:

$string = "Hello World!";
 echo "Given string: " . $string . "\n";
 echo "Updated string: " . rtrim($string, "!") . "\n";

And, the output is the following:

Given string: Hello World!
Updated string: Hello World

So, in this snippet, we discussed three ways of removing the last character from a string in PHP. These are simple actions that will help you to deal with one of the common issues in PHP.

Topic: PHP / MySQLPrev|Next

Answer: Use the rtrim() Function

You can simply use the rtrim() function to remove the last character from a string.

Let's take a look at an example to understand how it actually works:

<?php
// Sample string
$str1 = "Hello World!";

echo rtrim($str1, "!");  // Outputs: Hello World
echo "<br>";

// Sample string
$str2 = "red, green, blue, ";

echo rtrim($str2, ", ");  // Outputs: red, green, blue
?>


Here are some more FAQ related to this topic:

  • How to remove white space from a string in PHP
  • How to remove special characters from a string in PHP
  • How to extract substring from a string in PHP

Get Last Character Of String Php With Code Examples

In this session, we are going to try to solve the Get Last Character Of String Php puzzle by using the computer language. The code that follows serves as an illustration of this point.

substr("testers", -1); // returns "s"

You’ll see some examples of different ways to solve the Get Last Character Of String Php problem further down in this article.

substr("testers", -1); // returns "s"
//Or, for multibyte strings :
mb_substr("multibyte string…", -1); // returns "…"
function getLastWord($string)
    {
        $string = explode(' ', $string);
        $last_word = array_pop($string);
        return $last_word;
    }
phpCopy<?php  
$string = 'This is a string';
$lastChar = substr($string, -1);
echo "The last char of the string is $lastChar.";
?>
phpCopy<?php  
$string = "This is a string";

$lastChar = $string[-1];
echo "The last char of the string is $lastChar.";
?>
$split = explode(" ", $string);

echo $split[count($split)-1];

As we have seen, a large number of examples were utilised in order to solve the Get Last Character Of String Php problem that was present.

How do I get the last character of a string in PHP?

The strlen() is a built-in function in PHP which returns the length of a given string. It takes a string as a parameter and returns its length. It calculates the length of the string including all the whitespaces and special characters.21-May-2021

How do you get the last character of a string?

To get the last character of a string, use bracket notation to access the string at the last index, e.g. str[str. length – 1] . Indexes are zero-based, so the index of the last character in the string is str.25-Jul-2022

How do I cut a string after a specific character in PHP?

The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.21-Feb-2019

How do I get the first character of a string in PHP?

To get the first character from a string, we can use the substr() function by passing 0,1 as second and third arguments in PHP.19-Aug-2020

What is the purpose of $_ Php_self?

The $_SERVER["PHP_SELF"] is a super global variable that returns the filename of the currently executing script. So, the $_SERVER["PHP_SELF"] sends the submitted form data to the page itself, instead of jumping to a different page. This way, the user will get error messages on the same page as the form.

What is Substr function in PHP?

substr in PHP is a built-in function used to extract a part of the given string. The function returns the substring specified by the start and length parameter. It is supported by PHP 4 and above. Let us see how we can use substr() to cut a portion of the string.11-Jun-2021

How do substring () and substr () differ?

The difference between substring() and substr() The two parameters of substr() are start and length , while for substring() , they are start and end . substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 .13-Sept-2022

How do I find a character in a string?

The strchr() function finds the first occurrence of a character in a string. The character c can be the null character (\0); the ending null character of string is included in the search. The strchr() function operates on null-ended strings.

How do you get the first character of a string?

To get the first and last characters of a string, access the string at the first and last indexes. For example, str[0] returns the first character, whereas str[str. length – 1] returns the last character of the string.07-Oct-2021

Is substring in string PHP?

You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

How can I get the last part of a string in PHP?

Using substr() Method: The substr() is a built-in function in PHP that is used to extract a part of string. Example: For example, if the string is “Akshit loves GeeksForGeeks”. The last character of the string is “s”.

How do you get the last character of a string?

To get the last character of a string, use bracket notation to access the string at the last index, e.g. str[str. length - 1] . Indexes are zero-based, so the index of the last character in the string is str.

How do I get the last 5 characters of a string?

To get the last N characters of a string, call the slice method on the string, passing in -n as a parameter, e.g. str. slice(-3) returns a new string containing the last 3 characters of the original string.

How can I get the last word in PHP?

strrpos() Function: This inbuilt function in PHP is used to find the last position of string in original or in another string. It returns the integer value corresponding to position of last occurrence of the string, also it treats uppercase and lowercase characters uniquely.