douqi0090 2016-08-17 15:44
浏览 94

如何在第三次出现的事情上拆分字符串?

I know "explode" splits the string and turns it into an array for every occurrence. But how do I split on the third occurrence and keep everything after the third occurrence?

Examples 1:

$split = explode(':', 'abc-def-ghi::State.32.1.14.16.5:A);

I would like this to output:

echo $split[0];  // abc-def-ghi::State.32.1.14.16.5
echo $split[1];  // A

Examples 2:

$split = explode(':', 'def-ghi::yellow:abc::def:B);

I would like this to output:

echo $split[0];  // def-ghi::yellow
echo $split[1];  // abc::def:B
  • 写回答

3条回答 默认 最新

  • dongtanxi5676756 2016-08-22 15:30
    关注

    Explanation:

    First split by :: and get both the output
    Now, split further output by : and get individual strings
    Last, append required strings according to your requirements to get exact output.
    

    Code:

    <?php
      $str = "abc-def-ghi::State.32.1.14.16.5:A";
      $split1 = explode('::', $str)[0];
      $split2 = explode('::', $str)[1];
      $split3 = explode(':', $split2)[0];
      $split4 = explode(':', $split2)[1];
      echo $split1 . "::" . $split3;
      echo $split4;
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包