Search code examples
phpregexarraysstringinvisible

Why is a called 'string' invisible to an array?


The file below, can be setup using any JPG file from PhotoShop that has XMP data. In the 'pattern', replace 'eat:' with 'dc:' or any namespace returned from the '$string'.

Calling $string (1) using following array setup it produces a print_r array that looks like: (2)

If you uncomment the line ubove (1a), it will print to the browse, copy & paste into the line below (1a). this should produce an array that looks like: (3)

Why the difference print_r readings, when it's the same string?

How do I get it to behave like (3); ... better yet how do I make it end up like the(4)?

<?php
header("Content-Type: text/html; charset=utf-8");
$filename = "2012-04-24_WestCoast_2.jpg";
echo '<img src="'. $filename . '" alt="'. $filename . '" title="' .     $filename . '" width="350" /><p />';
$source = file_get_contents($filename);
$xmpdata_start = strpos($source,'<x:xmpmeta');
$xmpdata_end = strpos($source,"</rdf:Description>");
$xmplenght = $xmpdata_end-$xmpdata_start;
$xmpdata = substr($source,$xmpdata_start,$xmplenght+18);
$string = htmlentities($xmpdata); //(1)

//if (is_string($string)) {
//    echo "is a string\n"; //TRUE
//} else {
//    echo "is not a string\n";
//}

//$string = print_r("'".$string."';");
// (1a)=====================================
//$string = '<x:xmpmeta xmlns: === Truncated for simplicity ===x="adobe:ns:meta/" x:xmptk="Adobe XMP Core    5.3-c011 66.145661, 2012/02/06-14:56:27 "> <rdf:RDF    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"></rdf:Description>';

$pattern = '/eat:(.*?)="(.*?)"/is';
preg_match_all($pattern, $string, $matches);
$group = array($matches[1], $matches[2]);
//    foreach($group as &$match);
echo '<pre>';
// print_r ($match);
print_r ($group);
echo '</pre>';
?>

(2)=====================================
// If i just call the '$string', this is what i get:

Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
        )

)

(3)=====================================
// If I uncomment (1), the '$string' that I pasted inside the file, i get this:

Array
(
    [0] => Array
        (
            [0] => Biography
            [1] => Title
            [2] => object_description
            [3] => Medium
            [4] => in_height
            [5] => in_width
            [6] => in_depth
            [7] => Dated
            [8] => Photograph
        )

    [1] => Array
        (
            [0] => American B1942 Castine, Maine
            [1] => Reunion Dinner Party at the Slanted Door
            [2] => Nancy Freeman, Tim Patterson The Slanted Door San Francisco Calf.
            [3] => photography
            [4] => 2736
            [5] => 3648
            [6] => @ 240 dpi
            [7] => April 24, 2012
            [8] => PrimaryImage
        )

)

(4)=====================================
// This is what i'm trying to get too:

Biography: American B1942 Castine, Maine
Title: Reunion Dinner Party at the Slanted Door
object_description: Reunion Dinner Party at the Slanted Door
Nancy Freeman, Tim Patterson The Slanted Door San Francisco Calf.
Medium: photography
in_height: 2736
in_width: 3648
in_depth: @ 240 dpi
Dated: April 24, 2012
Photograph: PrimaryImage

Solution

  • Well it looks like i get to answer my own question of "Why it is a called 'string' invisible to an array?". The answer is: When it's preceded by a call to 'htmlentities()'.

    Now I'm not really sure why it happens but it does happen and the instant I went back and checked all my assumptions ... that 'htmlentities()' would clean up the raw string. Commenting out 'htmlentities()' made everything work.

    Is this a bug? I personally don't know, and i don't have the necessary experience using PHP to even hazard a guess. What i do know is it drove me up the wall for a week.