Ensure that the string ends with a specific character

From my times in Delphi I remember three functions (or routines) which were quite useful in working with directories and paths. They both are located in Delphi's SysUtils unit and are called IncludeTrailingBackslash and IncludeTrailingPathDelimiter.

Lately I was doing directory-related tasks and I decided that these functions would come in handy if I had them in PHP so I decided to create my own ports of them.

Let's create the main function at first. It will allow us to check for any character and not be limited to slashes.

  1. <?php
  2.  
  3. /**
  4.  * Ensure that the string ends with the specified character
  5.  *
  6.  * @param string $string String to validate
  7.  * @return string
  8.  */
  9. function includeTrailingCharacter($string, $character)
  10. {
  11.     if (strlen($string) > 0) {
  12.         if (substr($string, -1) !== $character) {
  13.             return $string . $character;
  14.         } else {
  15.             return $string;
  16.         }
  17.     } else {
  18.         return $character;
  19.     }
  20. }

As you can see there's nothing difficult in doing it - just check if the last character is not a $character then add it, otherwise return the original string.

Now it is easy to create wrapper functions checking for specific characters. Here's one for backslashes:

  1. <?php
  2.  
  3. /**
  4.  * Ensure that the string ends with backslash
  5.  *
  6.  * @param string $string String to validate
  7.  * @return string
  8.  */
  9. function includeTrailingBackslash($string)
  10. {
  11.     return includeTrailingCharacter($string, '\\');
  12. }

You can do exactly the same but with the forward slash:

  1. <?php
  2.  
  3. /**
  4.  * Ensure that the string ends with forward slash
  5.  *
  6.  * @param string $string String to validate
  7.  * @return string
  8.  */
  9. function includeTrailingForwardSlash($string)
  10. {
  11.     return includeTrailingCharacter($string, '/');
  12. }

In case you need to ensure that the string ends with the system-specific path delimiter (forward slash on *NIX systems and backslash on Windows systems), you can use PHP constant DIRECTORY_SEPARATOR instead of slashes:

  1. <?php
  2.  
  3. /**
  4.  * Ensure that the string ends with system specific directory separator
  5.  *
  6.  * @param string $string String to validate
  7.  * @return string
  8.  */
  9. function includeTrailingDirectorySeparator($string)
  10. {
  11.     return includeTrailingCharacter($string, DIRECTORY_SEPARATOR);
  12. }

That's pretty much it. Put it to good use :)

Comments
1
I get a PHP Notice when using your code...
"Notice: Uninitialized string offset:"
Caused by $string not being an array. 

To fix change..
$string[strlen($string)]
to....
substr($string, strlen($string) -1, 1)

So the complete function is:
function includeTrailingCharacter($string, $character)
{
	if (strlen($string) > 0) {
		if (substr($string, strlen($string) -1, 1) != $character) {
			return $string . $character;
		} else {
			return $string;
		}
	} else {
		return $character;
	}
}
Russell, February 3rd 2010, 13:59
2
1. The bracket syntax for strings starts counting from Zero (0) so, in your example you should use $string[strlen($string)-1].
But, that would fail on empty strings anyway.

2. To get the last character (or false on empty string) you can use: substr($string, -1);

if ($character === substr($string, -1)) {
 ....

Regards
Alejandro Lapeyre, March 20th 2010, 11:08
3
@Russel - I couldn't reproduce your error however it pointed me at the obvious error that I have introduced. Thanks!

@Alejandro - again, I'm surprised how I didn't think of this solution. Probably I was tired or in a hurry :) Your change implemented and code changed. Gracias!
Andris, April 22nd 2010, 11:56
4
So the code shown in the article has been fixed as suggested in the comments and is correct?
steve, August 16th 2011, 21:36
5
Yes, Steve, it has and is.
Andris, August 17th 2011, 20:13
6
You could have used one line of code in your method


return rtrim($string, $character) . $character;
John Doe, December 11th 2015, 12:24
Name
Email (required)
will not be published
Website
Recaptcha
you will only be required to fill it in once in this session

You can use [code][/code] tags in your comments