dongshi4078 2010-08-29 06:53
浏览 72
已采纳

将分隔符定期插入字符串中

I have the following string in php:

$string = 'FEDCBA9876543210';

The string can be have 2 or more (I mean more) hexadecimal characters I wanted to group string by 2 like :

$output_string = 'FE:DC:BA:98:76:54:32:10';

I wanted to use regex for that, I think I saw a way to do like "recursive regex" but I can't remember it.

Any help appreciated :)

  • 写回答

5条回答 默认 最新

  • douershuang7356 2010-08-29 06:57
    关注

    If you don't need to check the content, there is no use for regex.

    Try this

    $outputString = chunk_split($string, 2, ":");
    // generates: FE:DC:BA:98:76:54:32:10:
    

    You might need to remove the last ":".

    Or this :

    $outputString = implode(":", str_split($string, 2));
    // generates: FE:DC:BA:98:76:54:32:10
    

    Resources :

    On the same topic :

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用