【问题标题】:How to trim string from right in PHP?如何在 PHP 中从右侧修剪字符串?
【发布时间】:2017-12-07 07:15:05
【问题描述】:

我有一个字符串示例

this-is-the-example/exa

我想从上面的行中修剪 /exa

$string1 = "this-is-the-example/exa";
$string2 = "/exa";

我正在使用rtrim($string1, $sting2)

但是输出是this-is-the-exampl

我想this-is-the-example 作为输出。

这两个字符串都是动态的,并且可能在字符串中出现多次。但我只想删除最后一部分。 string2 中包含/ 也不是强制性的。这也可能是普通字符串。像aabc 也一样..

【问题讨论】:

    标签: php string trim


    【解决方案1】:

    您可以使用多种方法:

    substr(DEMO):

    function removeFromEnd($haystack, $needle)
    {
        $length = strlen($needle);
    
        if(substr($haystack, -$length) === $needle)
        {
            $haystack = substr($haystack, 0, -$length);
        }
        return $haystack;
    }
    
    
    $trim = '/exa';
    $str = 'this-is-the-example/exa';
    
    
    var_dump(removeFromEnd($str, $trim));
    

    使用正则表达式(DEMO):

    $trim = '/exa';
    $str = 'this-is-the-example/exa';
    
    function removeFromEnd($haystack, $needle)
    {
        $needle = preg_quote($needle, '/');
        $haystack = preg_replace("/$needle$/", '', $haystack);
        return $haystack;
    }
    var_dump(removeFromEnd($str, $trim));
    

    【讨论】:

      【解决方案2】:

      首先explode 字符串,使用array_pop 从分解数组中删除最后一个元素,然后使用/ 再次返回implode

      $str = "this-is-the-example/exa";
      if(strpos($str, '/') !== false)
      {
          $arr = explode('/', $str);
          array_pop($arr);
          $str = implode('/', $arr);
          // output this-is-the-example
      }
      

      如果您在 URL 中有多个 /,这将起作用,并且只会删除最后一个元素。

      $str = "this-is-the-example/somevalue/exa";
      
      if(strpos($str, '/') !== false)
      {
          $arr = explode('/', $str);
          array_pop($arr);
          $str = implode('/', $arr);
          // output this-is-the-example
      }
      

      【讨论】:

        【解决方案3】:

        你可以使用爆炸

        <?php
        
        $x = "this-is-the-example/exa";
        
        $y = explode('/', $x);
        
        echo $y[0];
        

        【讨论】:

        • "字符串是动态的,可能在字符串中出现多次" - 现在,如果有另一个 / 你不会剪切最后一部分
        • 是的,我明白了你的意思,我只是使用爆炸来获得所需的输出。
        • 这样我只能在 / 之前获得第一部分作为输出,如果替换字符串包含像'a'这样的计划字符
        【解决方案4】:

        rtrim的第二个参数是字符掩码而不是字符串,你最后的“e”被修剪了,这很正常。

        考虑使用其他东西,例如正则表达式 (preg_replace) 来满足您的需求

        这会保留 "/" char 之前的所有内容:

        $str = preg_replace('/^([^\/]*).*/','$1', 'this-is-the-example/exa');
        

        这会删除最后一部分。

        $str = preg_replace('/^(.*)\/.*$/','$1', 'this-is-the-example/exa/mple');
        

        【讨论】:

        • 我想从 / 开始修剪字符串不是强制性的。修剪字符串也可能有简单的字符。在少数情况下,装饰盒也可能是空的。
        【解决方案5】:

        希望这会有所帮助。 :)

        只需尝试以下代码:

        <?php
        $this_example = substr("this-is-the-example/exa", 0, -4);  
        echo "<br/>".$this_example; // returns "this-is-the-example"
        ?>
        

        【讨论】:

        • 两个字符串都是动态的,在少数情况下替换可能有时为空。并且可能是简单的 a、abc 或任何其他内容。
        【解决方案6】:

        strstr()打个招呼

        $str = 'this-is-the-example/exa';
        $trim = '/exa';
        $result = strstr($str, $trim, true);
        echo $result;
        

        【讨论】:

        • 字符串可能有多个/或替换字符串也可能有纯 abc。
        • 当 $trim = "a"; 时获取 'this-is-the-ex' 作为输出;
        【解决方案7】:

        为了允许错误处理,如果在搜索字符串中找不到子字符串...

        <?php
        
        $myString = 'this-is-the-example/exa';
        
        //[Edit: see comment below] use strrpos, not strpos, to find the LAST occurrence 
        $endPosition = strrpos($myString, '/exa');
        
        // TodO; if endPosition === False then handle error, substring not found 
        
        $leftPart = substr($myString, 0, $endPosition);
        
        echo($leftPart);
        
        ?>
        

        输出

        这就是例子

        【讨论】:

        • 当我将装饰盒更改为“a”时得到“this-is-the-ex”。
        • 我的错 - 如果您使用 builder() 而不是 strpos(),它将起作用。搜索字符串“a”不是唯一的。 strpos() 找到第一个出现,而 `strrpos() 找到最后一个,这就是你想要的。对于那个很抱歉;我已经更新了我的答案
        猜你喜欢
        • 2013-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多