Log in

View Full Version : a:1:{s:8:"uploaded";s:37:"128210178941769_1260783455_1263_q.jpg";}



smansakra
01-27-2011, 07:41 AM
i have a string like below
a:1:{s:8:"uploaded";s:37:"128210178941769_1260783455_1263_q.jpg";}

i want to get 128210178941769_1260783455_1263_q.jpg only,
can somebody here help me to do that??? :confused::confused::confused::confused::confused:


thanks so much....

james438
01-27-2011, 08:47 AM
Could you give more examples and details, otherwise the following works:


<?php
$text = 'a:1:{s:8:"uploaded";s:37:"128210178941769_1260783455_1263_q.jpg";}';
preg_match('/^.*\"([0-9]{7,}.*?)\"/',$text,$needle);
echo"$needle[1]";
?>

Basically, it looks for a double quote that is followed by a number that is 7 or more digits in length and then follows that all the way to the next quote matching everything between the two quotes then assigns it to the array $needle stored at $needle[1].

traq
01-27-2011, 03:21 PM
$serialized = 'a:1:{s:8:"uploaded";s:37:"128210178941769_1260783455_1263_q.jpg";}'
$array = unserialize($serialized);
print $array[0];
// 128210178941769_1260783455_1263_q.jpg

You serialize (http://us.php.net/manual/en/function.serialize.php) a value (any value, except a resource; and it's tricky -but possible- with an object) to make it storable (in a session or a database) and easily recoverable afterwards.

a:1 indicates an array containing 1 item.
:{ } shows the contents of the array.
s:8:"uploaded"; is the key (a string with 8 characters)
s:37:"128210178941769_1260783455_1263_q.jpg" is the value (a string with 37 characters).

smansakra
01-27-2011, 10:43 PM
@james438: thanks for reply
@trac : Thanks so much trac!!! , yes, i have seen 'serialize' and 'unserialize' but i don't know what is it and how to use it. ^_^
and now i know it... :D
thanks for explanation.......

smansakra
01-27-2011, 11:05 PM
trac, i have just tried your code

<?php
$serialized = 'a:1:{s:8:"uploaded";s:37:"128210178941769_1260783455_1263_q.jpg";}'
$array = unserialize($serialized);
print $array[0];
// 128210178941769_1260783455_1263_q.jpg
?>

and it returns Parse error: syntax error, unexpected T_VARIABLE in C:\Program Files\xampp\htdocs\abc.php on line 3

i think it the $serialized is a:1:{i:0;s:37:"128210178941769_1260783455_1263_q.jpg";}

but the variable above doesn't content i:0; :'(

djr33
01-27-2011, 11:49 PM
There's a missing semicolon at the end of the first line. Just add ;

traq
01-28-2011, 02:16 AM
yup, whoops - sorry about that.



BTW, " $array[0] " will also return an error. You'll need to use print $array['uploaded']; instead, sorry!

smansakra
01-28-2011, 04:27 AM
@djr33: thanks , yes i forgot 'semicolon' this morning,, hehehe ^_^
@trac: i works thanks so much