0

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

使用以下数组设置调用 $string (1) 它会生成一个 print_r 数组,如下所示: (2)

如果取消注释上面的行 (1a),它将打印到浏览器,复制并粘贴到下面的行 (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
4

2 回答 2

0

好吧,看起来我可以回答我自己的问题“为什么它是一个数组不可见的所谓的'字符串'?”。答案是:在调用“htmlentities()”之前。

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

这是一个错误吗?我个人不知道,而且我没有必要的使用 PHP 的经验来冒险猜测。我所知道的是它把我逼上了一个星期。

于 2012-09-06T13:42:15.357 回答
0

我不清楚在文件内部或外部设置字符串的问题是什么。目前尚不清楚您要解释什么。

array(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 时,我会使用代码来看看到底发生了什么。

于 2012-08-31T17:08:38.990 回答