php checking if the last character is a '/' if not then tack it on

Php

Php Problem Overview


I have these 2 snippets of code that I have been playing with, but can't seem to get the logic to stick in either of them.

I am trying to see if a given string has a '/' at the end, if not then add it.

$path = '.';

if (substr($path, 0, -1) != '/')
	$path .= '/';

and

if (strrpos('/', $path) !== true)
	$path .= '/';

the issue im having is that if i make $path equal to './ then I get this as the output .//

this is the snippet of where i am having the issue

if (!is_array($paths))
	$this->classPath[] = $paths;
else
	$this->classPath = $paths;

foreach ($this->classPath as $path) {

	if (strrpos('/', $path) !== true)// || substr_count($path, '/') >= 0)
		$path = $path . '/';
	//else
		//$this->classPath[] = $path;
		//echo '0';
	$pathArr[] = $path;

Php Solutions


Solution 1 - Php

You might be overthinking it. While the substr() method will work perfectly it might be simpler to use rtrim() to remove any trailing slashes and then add one on.

$path = rtrim($path, '/') . '/';

Caution: this will trim multiple trailing forward slashes. so .////// becomes ./

Solution 2 - Php

My solution: simple and even converts back slashes, useful for windows developers:

function fixpath($p) {
	$p=str_replace('\\','/',trim($p));
	return (substr($p,-1)!='/') ? $p.='/' : $p;
}

Solution 3 - Php

you can try to use this function

    function endsWith($FullStr, $needle)
    {
        $StrLen = strlen($needle);
        $FullStrEnd = substr($FullStr, strlen($FullStr) - $StrLen);
        return $FullStrEnd == $needle;
    }

taken from my blog post

then use it like

if (endsWith($path,'/') == false) 
{
    $path = $path."/";
}

and offcourse if you do not want to use above function to siplify things then you should change the way you are using substr

correct way to get last char is substr($path,-1)

Solution 4 - Php

You are just using substr wrong.

To get the last character you have just use negative offset: substr($path,-1)

Your code lacks 2 fundamental things, essential for the programming:

  • debugging
  • documentation reading.

Debugging is as simple as just echoing your variables.
by echoing substr($path, 0, -1) you can make yourself aware that your code is somewhat wrong and by reading documentation you can see the right usage.

Solution 5 - Php

you can also get a count of number of characters in a string by function strlen()

and for loop over and access, every index in string and do check if this index === '/'

do something like

$string = 'test/';
$countString = strlen($string);

for($i=0;$i < $countString;$i++){

    if ($string[$i] === '/'){
        echo 'yes string had /';
        die;
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEliView Question on Stackoverflow
Solution 1 - PhpMike BView Answer on Stackoverflow
Solution 2 - PhpcountachView Answer on Stackoverflow
Solution 3 - PhpJaspreet ChahalView Answer on Stackoverflow
Solution 4 - PhpYour Common SenseView Answer on Stackoverflow
Solution 5 - PhpAbdalla KaramView Answer on Stackoverflow