1

我正在尝试根据它包含的文件的后缀来评估一个字符串。

我需要区分仅包含图像文件(、、、、.png或)的字符串和包含图像和非图像文件混合的字符串。.gif.jpg.jpeg.bmp

我究竟做错了什么?

if (preg_match('~\.(png\)|gif\)|jpe?g\)|bmp\))~', $data->files)) {
  echo 'image only;'
} else {
  echo 'image + other types';
}

包含混合物的示例字符串:

filename 1 (https://example.com/test.pdf),
filename 2 (https://example.com/cool_image.jpg),
filename 3 (https://example.com/other-file.docx)

仅包含图像的示例字符串:

filename 1 (https://example.com/another.png),
filename 2 (https://example.com/cool_image.jpg)
4

3 回答 3

4

正则表达式是错误的。)每次扩展后你都有。这将起作用:

~\.(png|gif|jpe?g|bmp)~i

完整示例:

<?php
if (preg_match('~\.(png|gif|jpe?g|bmp)~i', "https://example.com/test.png")) {
  echo 'image only';
}
else {
  echo 'image + other types';
}

演示

使用更正的正则表达式,现在您可以检查该批文件是否仅包含图像、图像和文件,或者仅包含文件。我们已经完成了第一部分(检查是否有图像)。使用这个正则表达式,我们可以检查是否有非图像:

/^(?!.*[.](png|gif|jpe?g|bmp))(?:.*$|,)/im

它使用否定的前瞻来断言行中的扩展名不匹配。最后有一个非捕获组来检查行尾或逗号(以符合您的格式)。

所以最后,检查两个正则表达式并查看每个批次真正包含的内容:

$files=[
    'Non-Images Only'=>'filename 1 (https://example.com/test.exe)',
    'Mixed-Type'=>'filename 1 (https://example.com/test.pdf),
filename 2 (https://example.com/cool_image.jpg),
filename 3 (https://example.com/other-file.docx),
filename 4 (https://example.com/nice_image.png)',
    'Images-Only'=>'filename 1 (https://example.com/another.png),
filename 2 (https://example.com/cool_image.jpg))'];
foreach ($files as $type => $batch) {
    echo "Batch: ".$batch.PHP_EOL;
    echo "Expecting: ".$type.PHP_EOL;
    $images = preg_match('/\.(png|gif|jpe?g|bmp)/im', $batch);
    $nonImages = preg_match('/^(?!.*[.](png|gif|jpe?g|bmp))(?:.*$|,)/im', $batch);
    $result = "";
    if ($images && $nonImages) {
        $result = "Mixed-Type";
    }
    else {
        if ($images) {
            $result = "Images-Only";
        }
        else {
            $result = "Non-Images Only";
        }
    }
    echo "Result: ".$result.PHP_EOL;
    echo PHP_EOL;
}

注意:使用@mickmackusa 的测试列表

演示

于 2018-02-23T02:18:24.287 回答
1

你正在逃避你的括号,所以他们得到了字面上的对待。

您正在寻找的正则表达式很简单:~(\.png|gif|jpe?g|bmp)$~

if (preg_match('~(\.png|gif|jpe?g|bmp)$', $data->files)) {
  echo 'image only;'
}
else {
  echo 'image + other types';
}

请注意,$在末尾表示字符串的结尾是至关重要的;没有它,字符串的任何部分都将是有效匹配。因此,诸如此类的文件.jpg.exe将被视为“图像”。

(\.png|gif|jpe?g|bmp)$针对字符串运行正则表达式:

https://example.com/test.pdf
https://example.com/other-file.docx
https://example.com/cool_image.jpg.exe
https://example.com/cool_image.jpg

表明只有最后一个链接会匹配。

这可以在这里看到。

请注意,您可能还希望将i修饰符放在正则表达式的末尾,以允许大写的文件扩展名。这可以通过~(\.png|gif|jpe?g|bmp)$~i.

于 2018-02-23T02:19:08.987 回答
1

在阅读和重新阅读您的问题超过 20 次之后,我想我知道您要做什么。

对于每个字符串(一批文件),我运行两次preg_match()检查。一种查找后缀为pnggifjpgjpeg或的文件bmp。另一个在上述列表中寻找没有后缀的文件。

*注意:(*SKIP)(*FAIL)是一种用于匹配并立即取消模式中字符资格的技术。

代码: ( PHP Demo ) ( Image Pattern Demo ) ( Non-Image Pattern Demo )

$tests=[
    'Non-Images Only'=>'filename 1 (https://example.com/test.exe)',
    'Mixed-Type'=>'filename 1 (https://example.com/test.pdf),
filename 2 (https://example.com/cool_image.jpg),
filename 3 (https://example.com/other-file.docx),
filename 4 (https://example.com/nice_image.png)',
    'No Files'=>'filename 1 (),
filename 2 ()',
    'Images-Only'=>'filename 1 (https://example.com/another.png),
filename 2 (https://example.com/cool_image.jpg))'];

$image_pattern='~\.(?:png|gif|jpe?g|bmp)\),?$~im';
$non_image_pattern='~\.(?:(?:png|gif|jpe?g|bmp)(*SKIP)(*FAIL)|[^.)]+)\),?$~im';

foreach($tests as $type=>$string){
    echo "\t\tAssessing:\n---\n";
    echo "$string\n---\n";
    echo "Expecting: $type\n";
    echo "Assessed as: ";
    $has_image=preg_match($image_pattern,$string);
    $has_non_image=preg_match($non_image_pattern,$string);
    if($has_image){
        if($has_non_image){
            echo "Mix of image and non-image files";
        }else{
            echo "Purely image files";
        }
    }else{
        if($has_non_image){
            echo "Purely non-image files";
        }else{
            echo "No files recognized";
        }
    }
    echo "\n----------------------------------------------------\n";
}

输出:

        Assessing:
---
filename 1 (https://example.com/test.exe)
---
Expecting: Non-Images Only
Assessed as: Purely non-image files
----------------------------------------------------
        Assessing:
---
filename 1 (https://example.com/test.pdf),
filename 2 (https://example.com/cool_image.jpg),
filename 3 (https://example.com/other-file.docx),
filename 4 (https://example.com/nice_image.png)
---
Expecting: Mixed-Type
Assessed as: Mix of image and non-image files
----------------------------------------------------
        Assessing:
---
filename 1 (),
filename 2 ()
---
Expecting: No Files
Assessed as: No files recognized
----------------------------------------------------
        Assessing:
---
filename 1 (https://example.com/another.png),
filename 2 (https://example.com/cool_image.jpg))
---
Expecting: Images-Only
Assessed as: Purely image files
----------------------------------------------------
于 2018-02-23T07:42:46.087 回答