【问题标题】:Remove all characters after last instance of particular character在特定字符的最后一个实例之后删除所有字符
【发布时间】:2014-12-23 13:36:47
【问题描述】:

如何在最后一次出现斜线字符/ 后删除字符串中的所有内容?

例如,字符串是:

http://localhost/new-123-rugby/competition.php?croncode=12345678

我想删除最后一个/ 之后的所有内容,以便它只显示:

http://localhost/new-123-rugby/

但是/之后的内容可能是可变长度的。

请注意,URL 中可以有任意数量的斜杠。它需要能够在最后一个斜线之后删除内容。可能比上例中显示的更多。

【问题讨论】:

  • URL 是字符串还是要使用.htacess
  • @Rizier123 正如帖子中所说,URI是一个字符串。这是$链接
  • 你可以使用explode("/", $str)会给你一个数组
  • @Alex' 的解决方案简洁而恰当。您应该将其发布为答案。
  • 参考:parse_url

标签: php string substring


【解决方案1】:

你可以试试这个

$url = "http://localhost/new-123-rugby/competition.php?croncode=12345678";
preg_match("/[^\/]+$/", $url, $matches);
$newUrl = str_replace($matches[0],'',$url);
echo $newUrl;

【讨论】:

  • !错误! 正如@Francesca 在评论中所说:There could be any number of slashes 和 OP 想要以下输出:http://localhost/new-123-rugby/,但如果你有多个斜杠,你不会得到输出OP想要什么!请参阅:ideone.com/Q9ul6Q 如您所见,输出不是 OP 想要的!
  • @Rizier123,不要介意,请再读一遍。 OP提到她想“删除最后一个斜线后的内容”
【解决方案2】:

解决方案#1,使用substr() + strrpos()

$string = 'http://localhost/new-123-rugby/competition.php?croncode=12345678';
$pos = strrpos($string, '/');
if ($pos !== FALSE) {
    echo(substr($string, 0, $pos + 1));
}

函数strrpos() 查找/ 在字符串中最后一次 出现的位置,substr() 提取所需的子字符串。

缺点:如果$string 不包含'/'strrpos() 将返回FALSE 并且substr() 不会返回我们想要的。需要先检查strrpos()返回的值。

解决方案#2,使用explode() + implode()

$string = 'http://localhost/new-123-rugby/competition.php?croncode=12345678';
$array  = explode('/', $string);
if (count($array) > 1) {
    array_pop($array);               // ignore the returned value, we don't need it
    echo(implode('/', $array).'/');  // join the pieces back, add the last '/'
}

或者,我们可以将最后一个组件设为空而不是array_pop($array),并且无需在末尾添加额外的'/'

$string = 'http://localhost/new-123-rugby/competition.php?croncode=12345678';
$array  = explode('/', $string);
if (count($array) > 1) {
    $array[count($array) - 1] = '';  // empty the last component
    echo(implode('/', $array));  // join the pieces back
}

缺点(对于两个版本):如果$string 不包含'/'explode() 会生成一个包含单个值的数组,其余代码会生成'/' (第一段代码)或空字符串(第二段)。需要检查explode()产生的数组中的项数。

解决方案#3,使用preg_replace():

$string = 'http://localhost/new-123-rugby/competition.php?croncode=12345678';
echo(preg_replace('#/[^/]*$#', '/', $string));

缺点:无。当$string 包含'/' 和不包含'/'(在这种情况下它不会修改$string)时,它都能正常工作。

【讨论】:

    【解决方案3】:

    注意:

    对问题进行了编辑,因此原始答案(在编辑下方)与 OP 的要求不匹配。它没有被 OP 标记为编辑。


    编辑:

    更新了我的答案,使其现在符合 OP 的要求:

    (现在它可以使用任意数量的斜线)

    也可以使用
    http://localhost/new-123-rugby////////competition.php?croncode=12345678

    <?php
    
        $url = "http://localhost/new-123-rugby/competition.php?croncode=12345678";
        echo dirname($url) . "/";
    
    ?>
    

    输出:

    http://localhost/new-123-rugby/
    

    原答案:

    这应该适合你:

    <?php
    
        $string = "http://localhost/new-123-rugby/competition.php?croncode=12345678";
        echo $string = substr($string, 0, strpos(strrev($string), "/")-2);
    
    ?>
    

    输出:

    http://localhost/new-123-rugby/
    

    演示:http://ideone.com/0R9QUG

    【讨论】:

    • 它不起作用,因为使用了值 2。可以有任意数量的斜线。因此,为什么我要“最后一个”实例。最后一个实例可以是 3 个斜线、8、100...
    • @Francesca 如果你的问题从一开始就有这个就好了
    • @Rizier123 即使使用上面的内容,您提供的代码实际上也不起作用。例如,我刚刚对其进行了测试,它在“竞争”一词中途中断了。所以无论如何它都不起作用。如指定的那样,最后一个 / 之后的内容可以是可变长度的。
    • @Francesca 现在更新了我的答案,所以这正是你想要的,我让它尽可能简单!另外请将您问题中的编辑标记为“编辑”。
    猜你喜欢
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 2012-03-08
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多