14

I'm doing some url rewriting in PHP and need to find URLS with a slash at the end and then do a 301 redirect. I thought there'd be a simple PHP function to find last string, but I couldn't find anything. First instincts make m think I need to use regex, but I'm not 100%.

Here's one example:

http://domainx.com/characters/ I want to find a trailing slash and turn it into http://domainx.com/characters

So what function will help me check if the last character is a "/"?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Bob Cavezza
  • 2,810
  • 7
  • 38
  • 56
  • There are more than 10 ways of doing this! – Salman A Dec 13 '10 at 09:06
  • [`if (s($str)->endsWith('/'))`](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L117) [`header('Location: '.s($str)->replaceLast('/'))`](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L305), as found in [this standalone library](https://github.com/delight-im/PHP-Str). Anyway, for redirects, doing this in the web server (e.g. Apache using `mod_rewrite`) is probably easier. – caw Jul 27 '16 at 03:06

8 Answers8

51

A nice solution to remove safely the last / is to use

$string = rtrim($string, '/');

rtrim() removes all /s on the right side of the string when there is one or more.

You can also safely add exactly one single / at the end of an URL:

$string = rtrim($string, '/').'/';
powtac
  • 40,542
  • 28
  • 115
  • 170
33

You can use substr:

substr($str, -1)

This returns the last byte/character in a single-byte string. See also the multi-byte string variant mb_substr.

But if you just want to remove any trailing slashes, rtrim is probably the best solution.

And since you’re working with URLs, you might also take a look at parse_url to parse URLs as a trailing slash does not need to be part of the URL path.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
14

$string[strlen($string)-1] gives you the last character.

But if you want to strip trailing slashes, you can do $string = rtrim($string, '/');. If there is no trailing slash, $string will remain unchanged.

Adnan
  • 25,882
  • 18
  • 81
  • 110
rik
  • 8,592
  • 1
  • 26
  • 21
4

You can use basename()

This will return characters for http://domainx.com/characters/ as well as http://domainx.com/characters

You can do like this:-

$page = $_SERVER['REQUEST_URI'];
$module = basename($page);

Then you can use the $module directly in your conditional logic without doing any redirects.

If you want to collect the last / trimmed URL then you can do this:-

If you are storing the project base url in a config file:-

BASE_URL  = 'http://example.com'

then you can do this:-

$page = $_SERVER['REQUEST_URI'];
$module = basename($page);
$trimmedUrl = BASE_URL.'/'.$module;
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144
1

You could preg_replace() a / at the end of the subject

$url = 'http://domainx.com/characters/';
$url = preg_replace('/(?:\/)$/', '', $url);
Biotox
  • 1,563
  • 10
  • 15
1

If you have php > 7.1

$string[-1]

Will give you the last character

http://sandbox.onlinephpfunctions.com/code/ff439889f14906749e4eb6328796c354c60f269b

Difference between rtrim and custom function:

<?php

$string0 = 'hi//';
$string1 = 'hello/';
$string2 = 'world';

function untrailingslashit( $string ) {
    return $string[-1] === '/' ? substr( $string, 0, -1) : $string;
}

echo untrailingslashit($string0);
echo "\n";
echo untrailingslashit($string1);
echo "\n";
echo untrailingslashit($string2);
echo "\n";
echo rtrim($string0, "/");

Result:

hi/
hello
world
hi
nektobit
  • 843
  • 7
  • 11
0

With PHP 8

str_ends_with($string, '/');

New str_starts_with() and str_ends_with() functions are added into the core.

t_dom93
  • 10,226
  • 1
  • 52
  • 38
-1

This is coming straight from WordPress:

function untrailingslashit( $string ) {
    return rtrim( $string, '/\\' );
}
Ciprian
  • 872
  • 1
  • 10
  • 30
  • [`rtrim()`](https://stackoverflow.com/a/4427180/2943403) was already recommended on this page. – mickmackusa Aug 04 '22 at 07:21
  • But my solution is wrapped as a function, and it includes escaped slashes. It's also been tried and tested with millions of websites built on WordPress. – Ciprian Aug 04 '22 at 13:35
  • Wrapping a single, native function call in a custom function is not beneficial programming. There is only a requirement to rtrim forward slashes in this question. There is no call for rtrimming backslashes. – mickmackusa Aug 04 '22 at 21:12