-3

I want to split the string and get the specific data from this string. I am using MySQL and PHP.
I am having problems in retrieving data from database. Here is the string:

a:6:{s:12:"cfdb7_status";s:6:"unread";s:9:"your-name";s:12:"Talha Far";s:10:"your-email";s:19:"talha4@gmail.com";s:6:"number";s:11:"03379228";s:9:"your-city";s:9:"Islamabad";s:10:"Studylevel";s:8:"Graduate";}

I want to get these values from string:

Talha Far , talha4@gmail.com , 03379228, Islamabad , Graduate
Gholamali Irani
  • 4,391
  • 6
  • 28
  • 59
  • what have you tried so far? Retrieve the dta via mysql and unserialize the string using php – David Jan 27 '18 at 10:52

2 Answers2

0

You could use unserialize() to transform the string into an object :

$str = 'a:6:{s:12:"cfdb7_status";s:6:"unread";s:9:"your-name";s:9:"Talha Far";s:10:"your-email";s:16:"talha4@gmail.com";s:6:"number";s:8:"03379228";s:9:"your-city";s:9:"Islamabad";s:10:"Studylevel";s:8:"Graduate";}';
$obj = unserialize($str) ;
var_dump($obj);

And your wanted values :

echo $obj['your-name'];
echo $obj['your-email'];
echo $obj['number'];
// ...

But, be carefull, some indices are wrong. Note the differences between your given string and the string in this anwser (ex: s:9:"Talha Far" instead of s:12:"Talha Far").

Syscall
  • 19,327
  • 10
  • 37
  • 52
0
$str = 'a:6:{s:12:"cfdb7_status";s:6:"unread";s:9:"your-name";s:12:"Talha Far";s:10:"your-email";s:19:"talha4@gmail.com";s:6:"number";s:11:"03379228";s:9:"your-city";s:9:"Islamabad";s:10:"Studylevel";s:8:"Graduate";}';

$data = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $str);

print_r(unserialize($data));

You will get your data array from it and you can extract your values.

dferenc
  • 7,918
  • 12
  • 41
  • 49
DiggV
  • 1
  • 2