php 检查最后一个字符是否是“/”,如果不是,则添加它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9339619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:36:33  来源:igfitidea点击:

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

php

提问by Eli

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 = '.';

$path = '.';

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

and

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

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

我遇到的问题是,如果我$path等于'./那么我将其作为输出.//

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;

回答by Mike B

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.

你可能想多了。虽然该substr()方法可以完美运行,但使用rtrim()删除任何尾部斜杠然后添加一个可能更简单。

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

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

注意:这将修剪多个尾随正斜杠。于是.//////变成./

回答by countach

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

我的解决方案:简单甚至转换反斜杠,对 Windows 开发人员有用:

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

回答by Jaspreet Chahal

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 by 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

当然如果你不想使用上面的函数来简化事情,那么你应该改变你使用 substr 的方式

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

获取最后一个字符的正确方法是 substr($path,-1)

回答by Your Common Sense

You are just using substr wrong.

你只是使用 substr 错误。

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

要获得最后一个字符,您只需使用负偏移量: substr($path,-1)

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

你的代码缺少 2 个基本的东西,对于编程来说是必不可少的:

  • 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 documentationyou can see the right usage.

调试就像回显变量一样简单。
通过回显,substr($path, 0, -1)您可以让自己意识到您的代码有些错误,而通过阅读文档,您可以看到正确的用法。