PHP has several ways to give us the ability to manipulate strings such as user inputs, database values, or any other form of data.

Sometimes, you may have a specific long URL and need to remove the last slash from it. Fortunately, PHP provides several methods to do this task.

In the following section, you will learn how to use substr_replace. Let’s get started.

Removing the Last Character using the substr_replace

To do this task, there are 3 important arguments for using the substr_replace which are:

  • The original string value.
  • The replacement string will be added instead of the removed part.
  • The start position.
// Original string which has the URL
$string = "https://codedtag.com/?";

// Remove the last character
$updated = substr_replace($string, '', -1);

// Display the result
echo $updated; // https://codedtag.com/

In this example, we removed the ‘?’ mark from the URL. So, in the first argument, we passed the original string into the callback and then provided an empty value. Additionally, we specified the start position; if it is negative, it will start from the end of the string.

Let’s move on to the section below to learn about another method using the substr callback.

Removing the Last Character with substr

You can achieve the same result using the substr function to remove the last character from a string. Let’s see an example:

<?php

// Original string
$originalString = "Hello, World!";

// Remove the last character
$modifiedString = substr($originalString, 0, -1);

// Display the result
echo $modifiedString;

?>
PHP

In this example, substr is used to extract a portion of the original string, starting from the beginning (0) and excluding the last character (-1). This effectively removes the last character from the original string. The modified string is then echoed, and you should see “Hello, World” without the exclamation mark.

The mb_substr is another built-in function in PHP that can accomplish the same task. Let’s explore how it works in the section below.

Using mb_substr to Remove the Last Character from the String

mb_substr is a multi-byte safe version of substr in PHP, which is used for extracting parts of a string. To remove the last character from a string using mb_substr, you can follow the below example:

<?php 

$url = "https://codedtag.com/php/*";
echo mb_substr($url, 0, -1); 
// => https://codedtag.com/php/ 

?>
PHP

It prints the URL without the last character, which, in this case, is the asterisk (*). The mb_substr function is used to get a substring that includes all characters from the beginning of the string (position 0) to the second-to-last character (position -1).

Anyway, let’s understand how to achieve the same task using rtrim in PHP.

Removing the Last Character with rtrim in PHP

The rtrim() function in PHP is primarily used to remove whitespace or other characters from the end of a string. However, it can also be employed to remove a specific character by providing it as the second argument.

Let’s see an example:

<?php 

$originalString = "Hello, World!";
$modifiedString = rtrim($originalString, '!');

echo $modifiedString; // Output: Hello, World

?>
PHP

In this case, rtrim($originalString, '!') removes the exclamation mark from the end of the string.

Hoping this article has been helpful, let’s summarize it.

Wrapping Up

PHP offers developers a range of methods to remove the last character from strings, providing adaptability based on specific requirements. The exploration of these techniques has shed light on the diversity of options available.

Beginning with the substr_replace function, this method effectively replaces the last character with an empty string. This approach is particularly useful when a straightforward removal is desired.

Transitioning to another common technique, the substr function allows developers to extract a substring starting from the beginning and excluding the last character. This provides a concise way to eliminate the trailing character from a string.

For those working with multi-byte character sets, the mb_substr function emerges as a versatile alternative. This function ensures compatibility with multi-byte characters, offering a reliable means of removing the last character from a string.

Lastly, we considered the use of rtrim in a novel context. While traditionally employed to eliminate whitespace, it can be repurposed to remove a specific character from the end of a string. This flexibility enhances its utility in diverse string manipulation scenarios.

Thank you for reading, You can find more PHP tutorials here. or Visit PHP Manual.