dsds33222 2014-08-02 16:06
浏览 35
已采纳

在模式REGEX之后爆炸POST变量

I'm terrible at regex, hard to understand for me so I need some help. I have a variable which looks something like this:

["data:image/png;base64,Ivksfk...=", "data:image/png;base64,JksdkJkf...=", "data:image/png;base64,okKJjfeiw...="]

Those are 3 values which I want to split into an array. The way I see it, I want to split it at the comma which comes after a quote ", ". Can someone please help me with the regex for preg_split?

  • 写回答

3条回答 默认 最新

  • dsfw2154 2014-08-02 16:09
    关注

    You could try the below code to split the input string according to ", "

    <?php
    $yourstring = '["data:image/png;base64,Ivksfk...=", "data:image/png;base64,JksdkJkf...=", "data:image/png;base64,okKJjfeiw...="]';
    $regex = '~", "~';
    $splits = preg_split($regex, $yourstring);
    print_r($splits);
    ?>
    

    Output:

    Array
    (
        [0] => ["data:image/png;base64,Ivksfk...=
        [1] => data:image/png;base64,JksdkJkf...=
        [2] => data:image/png;base64,okKJjfeiw...="]
    )
    

    If you don't want "[,]" in the output then you could try the below code.

    <?php
    $data = '["data:image/png;base64,Ivksfk...=", "data:image/png;base64,JksdkJkf...=", "data:image/png;base64,okKJjfeiw...="]';
    $regex =  '~(?<=\["|", ")[^"]*~';
    preg_match_all($regex, $data, $matches);
    print_r($matches);
    ?>
    

    Output:

    Array
    (
        [0] => Array
            (
                [0] => data:image/png;base64,Ivksfk...=
                [1] => data:image/png;base64,JksdkJkf...=
                [2] => data:image/png;base64,okKJjfeiw...=
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?