帮助使用PHP正则表达式

时间:2011-02-24 17:27:14

标签: php regex

我有一个字符串,我想提取一部分,但我不熟悉正则表达式。 这是字符串:

<p>You have all kinds of great energy coming down today, and should be able to get almost anything started. It's one of those days when you need to be busy pretty much every waking minute!
</p> 
        <p>More horoscopes! Check your: <a href="http://horoscopes.astrology.com/index/dailysinglesindex.html?dst=rss%7Cast_horo%7Cdo">Daily Single's Love</a>, <a href="http://horoscopes.astrology.com/index/dailyromindex.html?dst=rss%7Cast_horo%7Cdo">Daily Couple's Love</a>, <a href="http://horoscopes.astrology.com/index/dailytechindex.html?dst=rss%7Cast_horo%7Cdo">Daily Work</a>, <a href="http://horoscopes.astrology.com/index/weeklyromindex.html?dst=rss%7Cast_horo%7Cdo">Weekly Romantic</a>, <a href="http://horoscopes.astrology.com/index/monthlyfitindex.html?dst=rss%7Cast_horo%7Cdo">Monthly Fitness</a>, <a href="http://horoscopes.astrology.com/?dst=rss%7Cast_horo%7Cdo">more</a> ...</p> 
        <p>Today's Free Sample Reading: Transform your love life or relationship in the coming year with our <a href="http://shop.astrology.com/scripts/runisa.dll?AO:TPROD::RSSHORODO,offer=null&dst=rss%7Cast_horo%7Cdo_offer&prodID=7014">free sample Love in the New Year tarot reading</a> at Astrology.com.</p> 
        <p><a href="http://www.ivillage.com/redir?iv_url=http://www.keen.com/documents/special_offers/astrology-lp1.asp?TID=FMkPKWEY">Is it really over? Find out if he'll come back with a Free Psychic Love Reading. </a></p><img src="http://feeds.feedburner.com/~r/dailyoverview/~4/-RSJe5GW1h0" height="1" width="1"/>

我只想提取第一段:

<p>You have all kinds of great energy coming down today, and should be able to get almost anything started. It's one of those days when you need to be busy pretty much every waking minute!
</p>

先谢谢,

3 个答案:

答案 0 :(得分:2)

你可以使用RegExp做到这一点,但不建议这样做。请参阅Parsing Html The Cthulhu Way

我可以预见你可能会获取一些HTML(或从文件中读取)并想要提取一些文本。 HTML用于布局,而不是用于数据存储的结构化语言。

答案 1 :(得分:2)

preg_match_all('/<p>.+?<\/p>/',$yourString, $result);

$p1 = $result[0][0];

print $p1;

仅当您的P标记为&lt; p&gt;内容时,此功能才有效。&lt; / p&gt;一旦开始添加属性,它就会中断。这就是为什么你不应该首先使用正则表达式来提取HTML的原因之一。

答案 2 :(得分:1)

您想要使用DOM解析器,而不是正则表达式。 Here's onehere's another