0

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:8:"Skeletal";i:1;s:6:"Cardio";i:2;s:8:"Muscular";}

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
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
DarkSpartan47
  • 153
  • 2
  • 16

3 Answers3

3

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);

adfaklsdjf
  • 250
  • 1
  • 11
  • @AbraCadaver I dunno, if I had asked the original question, assuming I had wanted a regex, I think I would have appreciated both answers -- the simpler (arguably more "correct") method, and the regex answer. – adfaklsdjf Mar 15 '17 at 22:35
  • 1
    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – AbraCadaver Mar 15 '17 at 22:36
  • Haha, yes, I have seen that specific answer before. It's *brilliant*! – adfaklsdjf Mar 15 '17 at 22:42
2

Those are serialized arrays, you need to unserialize them

gmc
  • 3,910
  • 2
  • 31
  • 44
0

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

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

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

Array
    (
        [0] => "Skeletal"
        [1] => "Cardio"
        [2] => "Muscular"
    )

I've created a working sample of this here.

Hope this helps! :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71