Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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 06:41 PM.

  • #2
    Regular Coder
    Join Date
    May 2011
    Posts
    241
    Thanks
    1
    Thanked 57 Times in 56 Posts
    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";
            }


  • Users who have thanked gvre for this post:

    afdah (08-31-2011)

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •