$s= <![CDATA[<p><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><img alt="diy_airplane_rudder.jpg" src="http://blog.makezine.com/diy_airplane_rudder.jpg" width="600" height="450" class="mt-image-none" style="" /></span></p>
<p>MAKE <a href="subscriberhttps://readerservices.makezine.com/MK/Subnew.aspx?pc=mk&pk=cmake">subscriber</a> James Jamison writes in to share his <a href="DIY">http://z12projects.blogspot.com/2010/08/rudder-pedals-on-cheap.html">DIY rudder pedals</a>. Wanting to add a bit more realism to a flight simulator, but not willing to spend much on a set of pedals, he was able to whip up a workable solution using some hardware and an old joystick. Looks good to me!</p>]]>
       <![CDATA[
<a href="http://blog.makezine.com/archive/2010/08/diy_rudder_pedals_on_the_cheap.html" target="_new">Read the Full Story »</a> | <a href="http://makezine.com" target="_new">More on MAKE »</a> | <a href="http://blog.makezine.com/archive/2010/08/diy_rudder_pedals_on_the_cheap.html#comments" target="_new">Comments »</a>]]>;
$s = strip_tags($s,'<img>');
$resault=$img[0][0];
echo $resault;
比如像上面一段代码,$s的内容可随意变化,上面的$s是我写出的其中一段供参考,大致格式就是这样。
我使用strip_tags函数取出里面的<img>函数的第一张图片,想把图片添加个CSS。
但高手们说strip_tags函数太弱,用正则比较方便。我正则水平不高,想求段代码。要求:1。先判断图片大小,小于100*100的图片忽略不取。
2。正则取出第一张图片,把图片修改为300*200像素的统一大小,照片外加个div, id为pic。先谢过了。

解决方案 »

  1.   

    <?php
    $s= <<<CONT
    <![CDATA[<p><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><img alt="diy_airplane_rudder.jpg" src="http://blog.makezine.com/diy_airplane_rudder.jpg" width="600" height="450" class="mt-image-none" style="" /></span></p>
    <p>MAKE <a href="subscriberhttps://readerservices.makezine.com/MK/Subnew.aspx?pc=mk&pk=cmake">subscriber</a> James Jamison writes in to share his <a href="DIY">http://z12projects.blogspot.com/2010/08/rudder-pedals-on-cheap.html">DIY rudder pedals</a>. Wanting to add a bit more realism to a flight simulator, but not willing to spend much on a set of pedals, he was able to whip up a workable solution using some hardware and an old joystick. Looks good to me!</p>]]>
           <![CDATA[
    <a href="http://blog.makezine.com/archive/2010/08/diy_rudder_pedals_on_the_cheap.html" target="_new">Read the Full Story »</a> | <a href="http://makezine.com" target="_new">More on MAKE »</a> | <a href="http://blog.makezine.com/archive/2010/08/diy_rudder_pedals_on_the_cheap.html#comments" target="_new">Comments »</a>]]>;
    CONT;
    preg_match_all('/<img[^>]*width=\"[1-9]\d{2}\" height=\"[1-9]\d{2}\"[^>]*\/>/is', $s, $matches);
    //print_r($matches);
    $firstimg = $matches[0][0];
    $newimg = preg_replace('/width=\"\d+\" height=\"\d+\"/', 'width="300" height="200"', $firstimg);
    echo "<id='pic' div>".$newimg."</div>";
    ?>
      

  2.   

    恩,正则修正一下,应该是
    preg_match_all('/<img[^>]*width=\"[1-9]\d{2,}\" height=\"[1-9]\d{2,}\"[^>]*\/>/is', $s, $matches);
      

  3.   

    好快,要是我也有这个熟练度就好了……
    太感谢CunningBoy了,你总是热心的帮助我们这些菜鸟们。