如何检查字符串只包含图像 url
How to check string contains only image urls
我正在尝试根据它包含的文件的后缀评估一个字符串。
我需要区分仅包含图像文件(.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)
正则表达式错误。每次扩展后您有 )
。这将起作用:
~\.(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';
}
使用更正后的正则表达式,现在您可以检查这批文件是仅包含图像、图像和文件,还是仅包含文件。我们已经完成了第一部分(检查是否有图像)。使用这个正则表达式,我们可以检查是否有 non-images:
/^(?!.*[.](png|gif|jpe?g|bmp))(?:.*$|,)/im
它使用否定前瞻来断言扩展名在行中不匹配。最后有一个 non-capturing 组来检查行尾或逗号(以符合您的格式)。
最后,检查两个正则表达式并查看每个批次真正包含的内容:
$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 的测试列表
你在逃避你的括号,所以他们得到了字面上的对待。
您正在查找的正则表达式很简单:~(\.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
之类的文件将被视为 'image'.
运行 正则表达式 (\.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
表明只有最后的 link 会匹配。
这可以看出有效here.
请注意,您可能还想在正则表达式的末尾添加 i
修饰符,以允许大写的文件扩展名。这可以通过 ~(\.png|gif|jpe?g|bmp)$~i
.
来完成
阅读 re-reading 你的问题超过 20 次后,我想我知道你想做什么。
对于每个字符串(文件批次),我 运行 两次 preg_match()
检查。一种搜索后缀为 png
、gif
、jpg
、jpeg
或 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
----------------------------------------------------
我正在尝试根据它包含的文件的后缀评估一个字符串。
我需要区分仅包含图像文件(.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)
正则表达式错误。每次扩展后您有 )
。这将起作用:
~\.(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';
}
使用更正后的正则表达式,现在您可以检查这批文件是仅包含图像、图像和文件,还是仅包含文件。我们已经完成了第一部分(检查是否有图像)。使用这个正则表达式,我们可以检查是否有 non-images:
/^(?!.*[.](png|gif|jpe?g|bmp))(?:.*$|,)/im
它使用否定前瞻来断言扩展名在行中不匹配。最后有一个 non-capturing 组来检查行尾或逗号(以符合您的格式)。
最后,检查两个正则表达式并查看每个批次真正包含的内容:
$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 的测试列表
你在逃避你的括号,所以他们得到了字面上的对待。
您正在查找的正则表达式很简单:~(\.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
之类的文件将被视为 'image'.
运行 正则表达式 (\.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
表明只有最后的 link 会匹配。
这可以看出有效here.
请注意,您可能还想在正则表达式的末尾添加 i
修饰符,以允许大写的文件扩展名。这可以通过 ~(\.png|gif|jpe?g|bmp)$~i
.
阅读 re-reading 你的问题超过 20 次后,我想我知道你想做什么。
对于每个字符串(文件批次),我 运行 两次 preg_match()
检查。一种搜索后缀为 png
、gif
、jpg
、jpeg
或 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
----------------------------------------------------