Web Analytics Made Easy -
StatCounter Extract value from string using preg_match - CodingForum

Announcement

Collapse
No announcement yet.

Extract value from string using preg_match

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Resolved 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; Aug 31, 2011, 01:41 PM.

  • #2
    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";
            }

    Comment


    • #3
      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.

      Comment

      Working...
      X