Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-31-2011, 10:52 AM   PM User | #1
afdah
New Coder

 
Join Date: Aug 2011
Posts: 10
Thanks: 6
Thanked 0 Times in 0 Posts
afdah is an unknown quantity at this point
Extract value from string using preg_match

Hi, I got the below string value from $data


Code:
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.

PHP Code:
$generated preg_match(''$data$matches);
$filename preg_match(''$data$matches); 

Last edited by afdah; 08-31-2011 at 05:41 PM..
afdah is offline   Reply With Quote
Old 08-31-2011, 12:48 PM   PM User | #2
gvre
Regular Coder

 
Join Date: May 2011
Posts: 212
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
PHP Code:
$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";
        }

gvre is offline   Reply With Quote
Users who have thanked gvre for this post:
afdah (08-31-2011)
Old 08-31-2011, 05:43 PM   PM User | #3
afdah
New Coder

 
Join Date: Aug 2011
Posts: 10
Thanks: 6
Thanked 0 Times in 0 Posts
afdah is an unknown quantity at this point
Thumbs up

Quote:
Originally Posted by gvre View Post
PHP Code:
$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.
afdah is offline   Reply With Quote
Reply

Bookmarks

Tags
preg_match

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:50 PM.


Advertisement
Log in to turn off these ads.