[英]PHP compare two arrays and filter array has specific values
有两个数组。
files 数组保存文件名。
内容数组保存 html 文本。
我想检查内容中未包含的过滤器文件。
$files = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"];
$contents = ["<p>random text</p>", "<p>another random text</p>", "<img src='1.jpg'>"];
我试过如下,但效果不佳。
$filter = [];
foreach ($contents as $key => $content) {
foreach($files as $key => $file) {
if(strpos($content, $file) == false) {
$filter[$key] = $file;
}
}
}
非常感谢。
看我的代码:
$files = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"];
$contents = ["<p>random text</p>", "<p>another random text</p>", "<img src='1.jpg'>"];
$included = [];
foreach ($contents as $content) {
foreach ($files as $file) {
if (strpos(strtolower($content), strtolower($file))) {
$included[] = $file;
}
}
}
print_r(array_diff($files, $included));
Array ( [1] => 2.jpg [2] => 3.jpg [3] => 4.jpg [4] => 5.jpg )
你可以试试这个:
foreach ($contents as $content) {
foreach($files as $file) {
if(!strpos($content, $file)) {
if (!in_array($file, $filter)) {
$filter[] = $file;
}
} else {
if (($key = array_search($file, $filter)) !== false) {
unset($filter[$key]);
}
}
}
}
尝试这个:
$files = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"];
$contents = ["<p>random text</p>", "<p>another random text</p>", "<img src='1.jpg'>"];
$alltxt="";
foreach($contents as $txt)$alltxt .=$txt;
$included = [];
foreach ($files as $file) {
if (strpos(strtolower($alltxt), strtolower($file))) {
$included[] = $file;
}
}
print_r(array_diff($files, $included));
结果:数组( [1] => 2.jpg [2] => 3.jpg [3] => 4.jpg [4] => 5.jpg)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.