0

我写这篇文章是因为我有这个问题:

我有一个带有 html 字符的字符串。

我想要的是从这个字符串中删除所有小于 10px 宽度的图像。我怎么能在 PHP 中做到这一点?

我一直在考虑使用一些循环,但我不知道如何实现它。有人可以帮助我吗?

4

2 回答 2

0

你可以试试

$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);

输出

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)
于 2012-09-27T19:57:51.900 回答
-1

我会尝试http://php.net/manual/en/function.getimagesize.php

于 2012-09-27T19:36:04.983 回答