php get string from binary file
I have JPG image with XMP meta data inside.
I'd like to read this data, but how?$content = file_get_contents($fileName);
var_dump($content);
displays real number of bytes 553700
but
$len = strlen($content);
var_dump($len);
displays 373821
So, I can't simple do
$xmpStart = strpos($content, '<x:xmpmeta');
because I get wrong offset.
So, the question is, how to find and read string开发者_JAVA技巧 from binary file in PHP?
(I have mb_string option ON
in php.ini)
UPD1:
I have some binary file. How can I check in PHP, this file contains several strings or not?
Essentially, it doesn't matter what kind of data you are reading - strlen()
et al. should always work.
What I think is happening here is that on your server, strlen()
is internally overridden by mb_strlen()
and the internal character encoding is set to UTF-8.
UTF-8 is a multi-byte encoding, so some of the characters in your (wildly arbitrary) byte stream get interpreated as multi-byte characters - resulting in a shortened length of 373821
instead of 553700
.
I can't think of a better workaround than always explicitly specifying a single-byte encoding like iso-8859-1
:
$pos = strpos($content, '<x:xmpmeta', 0, 'iso-8859-1');
this forces strpos()
(or rather, mb_strpos()
) to count every single byte in the data.
This will always work; I do not know whether there is a more elegant way to force the use of a single-byte encoding.
Getid3 is a PHP package that claims to be able to read XMP Metadata.
The exif_read_data() PHP function could help the XMP meta data
More info here: http://php.net/manual/en/function.exif-read-data.php
精彩评论