简体   繁体   English

PHP函数in_array无法识别变音符号

[英]PHP function in_array doesn't recognize diacritic

I have a code that runs through files and getting all images. 我有一个贯穿文件并获取所有图像的代码。

$img = '/srv/www/wordpress-default/public_html/wp-content/uploads/2018/07/2018_07_DogOwner_VS_CatOwner_655x368_NL-500x281.jpg';
$dir = preg_replace('#[^/]*$#', '', $img); 
$image_files = scandir($dir); 
$image_name = @array_pop(explode('/', $img));
$find = $image_name;
var_dump(in_array($find, $image_files)); 

In this example I run only through one image. 在此示例中,我仅运行一张图像。 This code returns true. 此代码返回true。 The problem is when I have an image that has for example german signs (hundezubehör-für-sommer.jpg). 问题是当我有一个带有德国标志的图像时(hundezubehör-für-sommer.jpg)。

$img = '/srv/www/wordpress-default/public_html/wp-content/uploads/2018/07/hundezubehör-für-sommer.jpg';

This returns false. 这将返回false。 Any ideas why this doesn't work? 任何想法为什么这不起作用?

EDITED: I have asked this question few days ago: How to find a shortest name (string) of the same image with different naming . 编辑:我几天前问过这个问题: 如何找到具有不同命名的同一图像的最短名称(字符串) The solution to this is here: https://3v4l.org/T7lfU . 解决方案在这里: https : //3v4l.org/T7lfU The problem I think is when I run the code from scandir then It can't find the diacritic. 我认为的问题是,当我从scandir运行代码时,它找不到变音符号。

The in_array function works despite the strings alphabet. 尽管有字符串字母, in_array函数仍然有效。 I guest the problem happens because your PHP file and filesystem use different encodings therefore the value read by scandir has another encoding therefore it differs from the $img value written in the code. 我接受这个问题是因为您的PHP文件和文件系统使用不同的编码,因此scandir读取的值具有另一种编码,因此它不同于代码中写入的$img值。

Try to convert the encoding of the scandir result to make it match the PHP file encoding. 尝试转换scandir结果的编码,使其与PHP文件编码匹配。 For example: 例如:

// ...
$image_files = scandir($dir);
foreach ($image_files as &$file) {
    $file = mb_convert_encoding($file, 'UTF-8', 'Windows-1251');
}
// ...
var_dump(in_array($find, $image_files)); 

Replace UTF-8 with the PHP file encoding and Windows-1251 with your filesystem encoding. 用PHP文件编码替换UTF-8 ,并用文件系统编码替换Windows-1251

The problem is with storing multi-byte characters like ö and ü into a PHP file. 问题在于将öü等多字节字符存储到PHP文件中。

You can try interpreting the string as multi-byte: 您可以尝试将字符串解释为多字节:

$img = utf8_encode('/srv/www/wordpress-default/public_html/wp-content/uploads/2018/07/hundezubehör-für-sommer.jpg');

Encoding, then decoding to make it safer: 编码,然后解码以使其更安全:

$img = html_entity_decode('/srv/www/wordpress-default/public_html/wp-content/uploads/2018/07/hundezubehör-für-sommer.jpg');

Or backslash the entities: 或反斜杠实体:

$img = "/srv/www/wordpress-default/public_html/wp-content/uploads/2018/07/hundezubeh\303\266r-f\303\274r-sommer.jpg";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM