【问题标题】:PHP - Return everything after delimiterPHP - 在分隔符后返回所有内容
【发布时间】:2017-02-19 21:21:39
【问题描述】:

SO 中有similar questions,但我找不到与此完全相同的。我需要删除直到(包括)特定分隔符的所有内容。例如,给定字符串File:MyFile.jpg,我需要删除直到: 的所有内容,这样我就只剩下MyFile.jpg。提前致谢!

【问题讨论】:

  • @SubirKumarSao 他在谈论 PHP
  • 我想到了substr,但是分隔符前的子串长度可能会有所不同。我想我需要将它与strpos 结合起来,但还没有找到最好的方法。
  • 已更正。当我注意到 php 标签时。

标签: php string


【解决方案1】:

使用这个 preg_replace 调用:

$str = 'File:MyFile.jpg';
$repl = preg_replace('/^[^:]*:/', '', $str); // MyFile.jpg

或者避免使用正则表达式并像这样使用explode:

$repl = explode(':', $str)[1]; // MyFile.jpg

编辑:使用这种方式来避免正则表达式(如果字符串中可以有多个:):

$arr = explode(':', 'File:MyFile.jpg:foo:bar');
unset($arr[0]);
$repl = implode(':', $arr); // MyFile.jpg:foo:bar

【讨论】:

  • 注意:explode(':', $str)[1] 在 PHP 中不起作用(至少到 5.3.8)
  • 绝对正确@Ejay,explode(':', $str)[1] 从 PHP 5.4 开始工作 :)
  • 这样一个简单任务的正则表达式?
  • 至于explode,如果有多个:就不行了,对吧?
  • @FelipeSchenone:如果字符串中可以有多个:,那么您可以使用 preg_replace 或使用我编辑过的答案进行爆炸。
【解决方案2】:

编辑:这个工作正常。

$str = "File:MyFile.jpg";
$str = substr( $str, ( $pos = strpos( $str, ':' ) ) === false ? 0 : $pos + 1 );

【讨论】:

  • 纯代码答案并没有真正的帮助。请添加一些解释。
  • 美丽的答案,谢谢。如果知道: 确实出现在字符串中,则可以简化为substr( $str, strpos( $str, ':' ) )
  • 这就是为什么使用另一个 substr :-)
【解决方案3】:

短代码:

要返回FIRST字符出现之前的所有内容,请使用strtok。示例:

  • strtok(16#/en/go, '#') 将返回 16

要返回FIRST字符出现后的所有内容,请使用strstr。示例:

  • strstr(16#/en/go, '#') 将返回 #/en/go(包括搜索字符 '#')
  • substr(strstr(16#/en/go, '#'), 1) 将返回 /en/go

要返回LAST字符出现后的所有内容,请使用strrchr。示例:

  • strrchr(16#/en/go, '/') 将返回 /go(包括搜索字符 '/')
  • substr(strrchr(16#/en/go/, '/'), 1) 将返回 go

【讨论】:

    【解决方案4】:

    您可以使用explode 来执行此操作:link

    类似:

    $string = "File:MyFile.jpg";
    list($protocol,$content) = explode(":", $string);
    echo $content;
    

    【讨论】:

      【解决方案5】:
          $str = "File:MyFile.jpg";
      
          $position = strpos($str, ':');//get position of ':'
      
          $filename= substr($str, $position+1);//get substring after this position
      

      【讨论】:

      • 是的,这应该是公认的答案......最明智、简短和高效。可简写为:substr($str, strrpos($str, ":")+1);
      • ...但是如果找不到分隔符,这将失败...这就是为什么 is 不应该是公认的答案。
      【解决方案6】:

      两种简单的方法:

      $filename = str_replace('File:', '', 'File:MyFile.jpg');
      

      $filename = explode(':', 'File:MyFile.jpg');
      $filename = $filename[1];
      

      【讨论】:

      • 第一个适用于示例,但不适用于一般情况。第二个似乎是一个答案!
      【解决方案7】:

      示例字符串:

      $string = 'value:90|custom:hey I am custom message|subtitute:array';
      

      字符串转数组

      $var = explode('|', $string);
      

      检查结果:

      Array(
      [0] => value:90
      [1] => custom:hey I am custom message
      [2] => subtitute:array)
      

      声明一个数组变量

      $pipe = array();
      

      遍历字符串数组$var

      foreach( $var as $key => $value ) {
        // get position of colon
        $position = strrpos( $value, ':' );
        // get the key
        $key = substr( $value, 0, $position );
        //get the value
        $value = substr( $value, $position + 1 );
        $pipe[$key] = $value; }
      

      最终结果:

      Array(
      [value] => 90
      [custom] => hey I am custom message
      [subtitute] => array)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-11
        • 2017-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-12
        • 1970-01-01
        相关资源
        最近更新 更多