Php 删除字符串中较小的图像
我写这篇文章是因为我有这个问题: 我有一个带有html字符的字符串 我想从这个字符串中删除所有小于10px宽度的图像。我如何在PHP中做到这一点 我一直在考虑使用一些循环,但我不知道如何实现它。有人能帮我吗?我会试试的你可以试试Php 删除字符串中较小的图像,php,image,detect,Php,Image,Detect,我写这篇文章是因为我有这个问题: 我有一个带有html字符的字符串 我想从这个字符串中删除所有小于10px宽度的图像。我如何在PHP中做到这一点 我一直在考虑使用一些循环,但我不知道如何实现它。有人能帮我吗?我会试试的你可以试试 $final = array(); $images = array(); $template = "<img src='%d.jpg' height='%d' width='%d' />"; for($i = 0; $i < 10; $i++)
$final = array();
$images = array();
$template = "<img src='%d.jpg' height='%d' width='%d' />";
for($i = 0; $i < 10; $i++)
{
$height = mt_rand(9, 12);
$weight = mt_rand(9, 12);
$images[] = sprintf($template , $i,$height,$weight);
}
var_dump($images);
foreach($images as $image)
{
$attribute = simplexml_load_string($image);
$attribute = $attribute->attributes();
if((int) $attribute['height'] < 10 or (int) $attribute['width'] < 10)
continue ;
$final[] = $image;
}
var_dump($final);
$final=array();
$images=array();
$template=“”;
对于($i=0;$i<10;$i++)
{
$height=mt_rand(9,12);
$weight=百万兰特(9,12);
$images[]=sprintf($template,$i,$height,$weight);
}
var_dump($images);
foreach($images作为$image)
{
$attribute=simplexml\u load\u字符串($image);
$attribute=$attribute->attributes();
如果((int)$attribute['height']<10或(int)$attribute['width']<10)
继续;
$final[]=$image;
}
var_dump(最终);
输出
array
0 => string '<img src='0.jpg' height='11' width='9' />' (length=41)
1 => string '<img src='1.jpg' height='12' width='9' />' (length=41)
2 => string '<img src='2.jpg' height='10' width='11' />' (length=42)
3 => string '<img src='3.jpg' height='12' width='11' />' (length=42)
4 => string '<img src='4.jpg' height='11' width='9' />' (length=41)
5 => string '<img src='5.jpg' height='9' width='11' />' (length=41)
6 => string '<img src='6.jpg' height='9' width='9' />' (length=40)
7 => string '<img src='7.jpg' height='10' width='9' />' (length=41)
8 => string '<img src='8.jpg' height='12' width='9' />' (length=41)
9 => string '<img src='9.jpg' height='10' width='10' />' (length=42)
array
0 => string '<img src='2.jpg' height='10' width='11' />' (length=42)
1 => string '<img src='3.jpg' height='12' width='11' />' (length=42)
2 => string '<img src='9.jpg' height='10' width='10' />' (length=42)
数组
0=>字符串“”(长度=41)
1=>字符串“”(长度=41)
2=>字符串“”(长度=42)
3=>字符串“”(长度=42)
4=>字符串“”(长度=41)
5=>字符串“”(长度=41)
6=>字符串“”(长度=40)
7=>字符串“”(长度=41)
8=>字符串“”(长度=41)
9=>字符串“”(长度=42)
排列
0=>字符串“”(长度=42)
1=>字符串“”(长度=42)
2=>字符串“”(长度=42)
您应该提供字符串的内容,并要求正则表达式执行您想要的操作。PHP如何理解img
元素的显示大小?如果是服务器端,您可能想使用JavaScript,但如果没有上下文,我不知道该建议什么。这个问题与此非常类似:我的答案列出了几个用于搜索html元素的库。您可以在此基础上展开并获取维度属性以确定。。你说的这个问题有点难以回答。很多时候,图像的尺寸不嵌入img元素中。在这种情况下,您必须打开每个图像以确定其尺寸。这并不完全是我想要的,但它确实帮助了我。为了删除我不想要的图像,我可以使用preg_替换。非常感谢。