0

The string looks like this.

string(202) 
"{"_id":"2","_rev":"2-2251dd9912477d8a2b91c6fd5ce62faf","_attachments":{"creativecommons.pdf":{"content_type":"text/pdf","revpos":2,"digest":"md5-AyV8c76dsfqfGF1BBdQIIw==","length":280357,"stub":true}}} ` 

I guess preg_match_all is a way to do it but i do not yet know the syntax for it. Maby there is an other way. Some help would be appreciated.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • If you built that JSON String, Its a bad idea giving a class name of `creativecommons.pdf` as that is not a valid class name when json_decoded to a PHP object – RiggsFolly Apr 14 '16 at 09:04

2 Answers2

1

Your string is a JSON string. "Prettified" it looks like this:

{
    "_id": "2",
    "_rev": "2-2251dd9912477d8a2b91c6fd5ce62faf",
    "_attachments": {
        "creativecommons.pdf": {
            "content_type": "text/pdf",
            "revpos": 2,
            "digest": "md5-AyV8c76dsfqfGF1BBdQIIw==",
            "length": 280357,
            "stub": true
        }
    }
}

You can use json_decode($JSONstring, true); to make it a multidimensional array:

$decoded = json_decode($JSONstring, true);
$myLengthValue = $decoded['_attachments']['creativecommons.pdf']['length'];
echo $myLengthValue; //Returns 280357
Daan
  • 12,099
  • 6
  • 34
  • 51
-1

preg_match ought to do it

syntax would be preg_match('/length":(.*),/', $string, $array);

preg_match will put the results into an array, so in this case:

$array[0] should be 'length":280357,;',

$array[1] should be '280357'

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • As its a JSON String its a lot easier to `json_decode()` it and use array or object notation on it. _edit: Not my downvote_ Guys its a bit rough to DV a guy with 1 rep – RiggsFolly Apr 14 '16 at 08:55
  • This is definitely the wrong way to do it. Do not use a Regex for decoding a JSON encoded string. – Andreas Apr 14 '16 at 08:56
  • you're right, although at least he now knows the syntax for preg_match for next time he needs it, as he said he didn't know it in the question. – Mike Bricknell-Barlow Apr 14 '16 at 08:57