douchi8503 2011-04-23 13:29
浏览 43
已采纳

用于从PHP中的html代码中提取图像URL的正则表达式[重复]

Possible Duplicates:
Grabbing the href attribute of an A element
Best methods to parse HTML

I have been using this code to extract images from HTML code in PHP:

$output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
if ( $output > 0 ) echo $matches[1][0];

It has been working fine for me all the time but its misbehaving with a particular HTML code. I don't have good grip on regex so need help to figure this out.

Works for:

<p>
    I finally decided to try Pomodoro technique to see how well it can improve my productivity as I am a lot disorganised, lazy sorta geek (well who isn’t?). So I built up a small script which acts as a Pomodoro timer for me using <a href="http://blog.ashfame.com/2011/04/ubuntu-notification-system/">Ubuntu notification system</a> (Do read it if you haven’t, you need to install lib-notify package for this script to work).
</p>
<p>
    I have created a launcher in my top panel, with which I start a new <em>pomodori</em> (name for a new period of time, lets call it a Pomodoro anyway). It calls up the script which alerts me that a new Pomodoro (time period) has started and then alert me again when the timer ends and I should take a small break.
</p>
<p>
    Here is the script:
</p>
<pre class="brush: bash; title: ; toolbar: false;" title="">
 DISPLAY=:0 notify-send -t 1000 -i /home/ashfame/Dropbox/Ubuntu/icons/pomodoro.png "New Pomodoro starts" "You have 25 minutes to work."# 25 minutes timersleep 1500DISPLAY=:0 notify-send -t 1000 -i /home/ashfame/Dropbox/Ubuntu/icons/pomodoro.png "Pomodoro ends" "Take a break!"
</pre>
<p>
    As soon as I click the launcher, the first notification appears telling me that a new Pomodoro has started.
</p>
<p>
    <img class="aligncenter" src="http://blog.ashfame.com/wp-content/uploads/2011/04/pomodoro-starts.png" alt="pomodoro starts">
</p>
<p>
    Then it sleeps for 1500 secs = 25 minutes. And after that the second notification appears telling me that the Pomodoro has ended.
</p>
<p>
    <img class="aligncenter" src="http://blog.ashfame.com/wp-content/uploads/2011/04/pomodoro-ends.png" alt="pomodoro ends">
</p>
<p>
    I just take a 3-5 minutes break or even longer (I am the boss!), and then I again click on the launcher starting another Pomodoro and I work for another 25 minutes. You can use the same tomato icon, if you want.
</p>
<p>
    <img class="aligncenter" src="http://blog.ashfame.com/wp-content/uploads/2011/04/pomodoro.png" alt="pomodoro">
</p>
<p>
    Enjoy the awesomeness of Ubuntu and ditch Windows, yes I am an Ubuntu advocate and will push you to switch all the time <img src='http://blog.ashfame.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley'>
</p>

Doesn't Work for :

<p>
    <img style="margin: 0px 10px 5px 0px" src="http://ijew.com.br/wp-content/uploads/HLIC/5b8b8f82bd69fd4a78aa114fd91bd9b5.jpg" width="300" height="226">
</p>
<p>
    Hey ijews! Pessach é inesquecível! E quem pode esquecer comendo 8 dias matzá?!
</p>
<p>
    Produção caseira muito bem feita.
</p><!--more-->
<p>
    &nbsp;
</p>
<p>
    <iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/d3D6O_sBOlc?rel=0" frameborder="0" allowfullscreen=""></iframe>
</p>
  • 写回答

1条回答 默认 最新

  • duancan65665 2011-04-23 14:08
    关注

    Turn <img.+src to either <img.+?src (lazy mode) or - even better - to <img[^>]+src.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试