0

I have following string. I need to extract just the Model: or the Orientaiton, or the Software and store as a variable. The problem I'm having is I don't know how to get individual variables. Any advice?

Model:  NIKON D200
Orientation:    0 
Resolution: 300.000 
ResolutionUnit: 2 
Software:   Nikon View 6.2.7 M
Date and Time:  2007:02:16 14:41:53
Exposure Time:  1/640 Sec
ISO Speed:  210
F Number:   8.0
Flash:  Yes, Strobe detected
Zoom Length:    32 mm
Exposure Program:   Manual
ISO Speed:  200 
Exposure Bias:  0.000 
Metering Mode:  Spot
Light Source:   Unknown
Zoom Length:    32 mm
12
  • explode on each line, then for each line explode on the first colon (ie. explode on colon, limit 2). Trim both sides, and use a foreach to find the "key" you want. Commented Dec 13, 2013 at 3:58
  • 1
    What you have there is not JSON. Please post the actual JSON data so we can better understand exactly what is needed to get the data you want. Commented Dec 13, 2013 at 3:58
  • @winterblood OP clearly stated that it's not JSON. Commented Dec 13, 2013 at 3:58
  • 1
    @NiettheDarkAbsol To quote the question: "I have the following output in json". Commented Dec 13, 2013 at 3:59
  • @winterblood I fixed. I'm able to get everything as individual if it's an array, but not at ExifCamerainfo Commented Dec 13, 2013 at 4:00

1 Answer 1

2

Here's a simple function to do the conversion to an associative array.

// Convert a series of key:value lines to an associative array
function KeyValueLinesToAssocArray($lines) {
    $linesArray = explode("\n", $lines);
    print("Entries: " . count($linesArray) . "<br />");
    $assoc = array();
    foreach ($linesArray as $kv) {
        $kvs = explode(":", $kv, 2);
        if (2>count($kvs)) continue;
        $assoc[trim($kvs[0])] = trim($kvs[1]);
    }
    return $assoc;
}

$data = <<<DATA
Model:  NIKON D200
Orientation:    0 
Resolution: 300.000 
ResolutionUnit: 2 
Software:   Nikon View 6.2.7 M
Date and Time:  2007:02:16 14:41:53
Exposure Time:  1/640 Sec
ISO Speed:  210
F Number:   8.0
Flash:  Yes, Strobe detected
Zoom Length:    32 mm
Exposure Program:   Manual
ISO Speed:  200 
Exposure Bias:  0.000 
Metering Mode:  Spot
Light Source:   Unknown
Zoom Length:    32 mm
DATA;

// Now split the ExifCameraInfo string into an associative array
$exif = KeyValueLinesToAssocArray($data);

print $exif["Model"];
Sign up to request clarification or add additional context in comments.

5 Comments

I got that far; I want only the model or the orientation, etc. your solution prints out entire string, whereas I only want a single element. $camera = $decoded[0][IPTC]['ExifCameraInfo'];
Ah, nice of you to downvote when your question is so vague. But easily fixed: split the string by newlines, break on first colon, then extract the appropriate values.
Lol, no worries - somebody did! I think we're moving in different directions as you edit your post and I my answer ;-)
your above answer works great for the data variable you define above....for what i have it doesn't work for some reason...??
Umm... I copied the data from your question above. Modify the function to : ` function KeyValueLinesToAssocArray($lines) { $linesArray = explode("\n", $lines); print("Entries: " . count($linesArray) . "<br />");` - see modification in code above. What does it print?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.