...

View Full Version : Resolved Extract value from string using preg_match



afdah
08-31-2011, 10:52 AM
Hi, I got the below string value from $data



HTTP/1.1 200 OK X-Powered-By: PHP/5.3.6 Content-type: application/json Content-Length: 277 Date: Wed, 31 Aug 2011 09:39:18 GMT Server: lighttpd/2.0.0 {"links":[{"source":"http:\/\/www.filehost.com\/file\/abcdef","generated":"http:\/\/server10.fileserver.com\/files\/abcdef\/file.avi","filename":"file.avi","status":"OK","error":null,"time":0.174665}]}


How do I extract the "generated" and "filename" value by using preg_match?
I'm weak at the regular expressions part... :-)
Thank you.



$generated = preg_match('', $data, $matches);
$filename = preg_match('', $data, $matches);

gvre
08-31-2011, 12:48 PM
$pos = strpos($data, "{");
if ($pos !== false)
{
$data = json_decode(substr($data, $pos));
if (isset($data->links[0]->generated, $data->links[0]->filename))
{
$generated = $data->links[0]->generated;
$filename = $data->links[0]->filename;

echo "generated: $generated, filename: $filename\n";
}
}

afdah
08-31-2011, 05:43 PM
$pos = strpos($data, "{");
if ($pos !== false)
{
$data = json_decode(substr($data, $pos));
if (isset($data->links[0]->generated, $data->links[0]->filename))
{
$generated = $data->links[0]->generated;
$filename = $data->links[0]->filename;

echo "generated: $generated, filename: $filename\n";
}
}


Hey, thanks man. I didn't know there is a json_decode function in PHP.
I was using preg_match function to extract the values from the string.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum