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: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
</div>
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);
Those are serialized arrays, you need to unserialize them
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! :)