2017-03-15 22:23
浏览 32

如何用PHP过滤字符串? [重复]

This question already has an answer here:

I have a plugin exporting a bunch of strings from a database for me. The data returned is in a format like this


a:3:{i:0;s:14:"Access to care";i:1;s:15:"Confidentiality";i:2;s:20:"Consent to treatment";}

I can apply a php function to filter the data, how would I get it to return like this, using a function.

Skeletal, Cardio, Muscular

Access to care, Confidentiality, Consent to treatment

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • 这是什么字符串? 如何反序列化此字符串? [复制] 2 answers \ n

    我有一个插件从我的数据库中导出一串字符串。 返回的数据格式如下

      a:3:{i:0; s:8:“Skeletal”; i:1; s:6:“Cardio  “; i:2; s:8:”肌肉“;} 
    a:3:{i:0; s:14:”获得护理“; i:1; s:15:”保密“; i  :2; s:20:“同意处理”;} 

    我可以应用php函数来过滤数据,我怎样才能让它返回 这个,使用一个函数。

     <  / DIV>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dream8877 2017-03-15 22:26

    Those strings are serialized variables. Arrays specifically. use unserialize() to get the array and then join() to comma separate it the way you want.

    $unserialized_array = unserialize($string);
    $comma_separated = join(', ', $unserialized_array);
    echo $comma_separated;

    edit: I think this is the simplest solution, but Obsidian Age's answer below provides a regex that will also do what you want (and you did ask for a regex);

    解决 无用
    打赏 举报
  • duanqiaoren9975 2017-03-15 22:26

    Those are serialized arrays, you need to unserialize them

    解决 无用
    打赏 举报
  • dongzanxun2790 2017-03-15 22:28

    You can do this with a really simple preg_match_all() of /".*?"/:

    $string = 'a:3:{i:0;s:8:"Skeletal";i:1;s:6:"Cardio";i:2;s:8:"Muscular";}';
    preg_match_all('/".*?"/', $string, $matches);

    Now $matches contains an array that you can simply access the indexes of:

            [0] => "Skeletal"
            [1] => "Cardio"
            [2] => "Muscular"

    I've created a working sample of this here.

    Hope this helps! :)

    解决 无用
    打赏 举报

相关推荐 更多相似问题