【问题标题】:Remove duplicate trailing slashes删除重复的尾部斜杠
【发布时间】:2012-12-09 00:58:33
【问题描述】:

我想用php 检测像$string 这样的字符串是否包含重复的尾部斜杠。

例如:

$string = "http://somepage.com/something/some.html/////";

$string = "http://somepage.com/something/some.html";

我想做一个if,如果它有重复,比如:

If ($string = "http://somepage.com/something/some.html/////";) {
    remove extra trailing slashes
} 
//else do nothing... 

【问题讨论】:

标签: php regex string url


【解决方案1】:

像这样申请rtrim

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

【讨论】:

    【解决方案2】:

    你可以使用rtrim():

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

    如果你出于某种原因想先检查它是否有斜杠,那么你可以检查最后一个字符,如下所示:

    if ($string[ strlen($string)-1 ] === '/') {
        $string = rtrim($string, '/');
    }
    

    通过rtrim() 传递字符串并不昂贵,因此您实际上不必首先检查尾部斜杠。

    使用正则表达式修剪尾部斜线有点过头了。

    【讨论】:

    • if ($string[ strlen($string)-1 ] === '/') { $string = rtrim($string, '/'); } .. $string[ strlen($string)-1 ] 是什么意思... 为什么 $string 是一个数组? ...我不明白,但它看起来对我来说是最好的解决方案。我有两种类型的 URL ......例如:mikrobusz-berles.commikrobusz-berles.com/flotta.html ......我只想删除 ebery 多余的斜线......在 .html 很容易,但是当我写的时候:@987654323 @ ?
    • 您可以使用[] 访问字符串中的字符。 rtrim($string, '/') 删除右侧的所有正斜杠。在提出更多问题之前,请参阅documentation
    【解决方案3】:
    $string = rtrim($string, '/');
    

    【讨论】:

      【解决方案4】:

      rtrim 是最好的解决方案,但由于您为完整性标记了regex

      $string = "http://somepage.com/something/some.html/////";
      echo preg_replace('#/+$#','',$string);
      
      >>> http://somepage.com/something/some.html
      

      #   - Is the delimiter character 
      /+  - Matches one or more forward slash
      $   - Matches the end of the string
      #   - Delimiter 
      Replace with 
      ''  - Nothing (empty string)
      

      【讨论】:

        【解决方案5】:

        有些地方/ 可以重复,例如,您可以通过所有这些链接访问您的问题:

        这里唯一不同的 /http://,所以让我们考虑一下。 rtrim 在我提供的大多数情况下都不起作用,所以让我们使用正则表达式。

        解决方案

        $parts = explode('//', $full_url, 2);
        $parts[1] = rtrim(preg_replace('@/+@', '/', $parts[1]), '/');
        $full_url = implode('//', $parts);
        unset($parts);
        

        现场测试:http://ideone.com/1qHR9o

        Before: https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes/
        After:  https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes
        ---------------------
        Before: https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes////
        After:  https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes
        ---------------------
        Before: https://stackoverflow.com///questions///13990256///remove-duplicate-trailing-slashes////
        After:  https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes
        ---------------------
        Before: https://stackoverflow.com/questions//13990256/remove-duplicate-trailing-slashes//
        After:  https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes
        ---------------------
        

        说明

        根据您的问题,我了解到您始终会获得完整的网址,因此,我们可以将其分为两部分:

        $parts = explode('//', $full_url, 2);
        

        现在我们删除重复的/

        preg_replace('@/+@', '/', $parts[1])
        

        然后我们去掉字符串末尾多余的/

        $parts[1] = rtrim( /*previous line*/ , '/');
        

        然后将其内爆:

        $full_url = implode('//', $parts);
        unset($parts);
        

        【讨论】:

        • 我不知道,我投票给你来补偿 :) 虽然爆炸部分有点棘手。我更喜欢排除正则表达式,它只是排除https?:// 部分。因为,如果 url 中没有定义协议怎么办?那么它就会失败。
        • @Jelmer 实际上,如果没有定义协议,那么你就没有 URL,你有一个 URI 并且数据将是无效的,但是可以通过检查是否数组的大小为 2,虽然我在收到时测试参数是否有效。
        • 所有 OP 想要的是删除字符串中的 尾随 正斜杠,rtrim 是完美的解决方案,您回答的问题是验证 @ 中的正斜杠987654341@.
        • @sudo_O 好的,我不知道尾随的意思是“在右边”,我一直把它理解为多个或顺序。那是语言障碍。
        • @JoséRobertoAraújoJúnior 嗯,好的,是的,trailing 的意思是 found at the end
        猜你喜欢
        • 2016-01-10
        • 1970-01-01
        • 1970-01-01
        • 2015-06-20
        • 2015-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多