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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程