可能重复:
Grabbing the href attribute of an A element
Best methods to parse HTML
我一直在使用此代码从PHP中的HTML代码中提取图像:
$output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
if ( $output > 0 ) echo $matches[1][0];
它一直对我很好,但它对特定的HTML代码行为不端。我对正则表达式没有很好的掌握,所以需要帮助解决这个问题。
适用于:
<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>
不适用于:
<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>
</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>
答案 0 :(得分:3)
将<img.+src
转为<img.+?src
(懒惰模式)或 - 甚至更好 - 转为<img[^>]+src
。