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.

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

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊