为什么一个叫';字符串';数组不可见


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

下面的文件可以使用PhotoShop中任何具有XMP数据的JPG文件进行设置。在"pattern"中,将"eat:"替换为"dc:"或从"$string"返回的任何命名空间。

使用以下数组设置调用$string(1),它会生成一个print_r数组,看起来像:(2)

如果取消对ubove(1a)行的注释,它将打印到browse,copy&粘贴到下面的行(1a)。这应该会产生一个数组,看起来像:(3)

当它是相同的字符串时,为什么print_r读数不同?

我该如何让它表现得像(3)。。。更好的是,我如何让它像(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) =========================================
//如果我只是调用"$string",这就是我得到的:

Array
(
    [0] => Array
        (
        )
    [1] => Array
        (
        )
)

(3) =========================================
//如果我取消注释(1),即我粘贴在文件中的"$string",我会得到:

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) =========================================
//这也是我想要得到的:

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

我不清楚在文件内外设置字符串的问题是什么。目前还不清楚你想解释什么。

数组(3)的输出是由正则表达式中的括号引起的。我不知道确切的原因,但为了得到你想要的结果(4),你可以使用一个循环将两个数组连接到一个新的数组中。

注意:您使用$group所做的就是制作一个数组数组。您没有将两个数组合并为一个数组。要合并数组,您需要遍历两个数组,并将每个元素合并为新数组的新元素。

示例:

for($i=0; $i<match[0].length; $i++){
    $result[i] = $match[0][i]. ": " .$match[1][i];
}

我对php有些生疏,但这就是合并数组的想法。

--编辑--

现在我明白了你在做什么,我看到了两个可能出现问题的地方。首先是:你是否100%确定你使用的图像在你想要的字段中包含任何元数据?我不确定元数据的确切性质,但我知道会有计算机所需的数据集(你过滤掉的起点和终点),以及自定义数据。如果为空,它甚至可能不包括在内。

第二个可能的问题是如何对元数据进行分块。也许可以试着写正则表达式来去掉字符串的开头和结尾。将一组字符前面的所有内容替换为",并对后面执行类似的表达式。

另外,您为$group设置的变量与$match完全相同。您将数组%match中包含的两个数组分配给一个名为$group的数组。仅供参考。使用我之前发布的sudo代码来设计将两个数组合并为一个的循环。

祝你好运。也许当我在测试计算机上设置php时,我会玩代码,看看到底发生了什么。

看起来我可以回答我自己的问题"为什么它是一个数组不可见的名为‘string’的字符串?"。答案是:当它前面有一个对"htmlenties()"的调用时。

现在我真的不确定为什么会发生,但它确实发生了,当我回去检查所有假设的那一刻。。。"htmlenties()"将清理原始字符串。注释"htmlentities()"使一切正常。

这是个虫子吗?我个人不知道,而且我没有使用PHP的必要经验,甚至没有猜测的风险。我所知道的是,它把我逼了一个星期。