2011-02-24 5 views
0

J'ai une chaîne et je veux en extraire une partie, mais je ne suis pas familier avec Regex. C'est la chaîne:Aide en utilisant PHP Expression régulière

<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"/> 

Je veux extraire le premier paragraphe que:

<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> 

Merci à l'avance,

+0

Il ya une page wiki sympa - http://stackoverflow.com/questions/3650125/how-to-parse-html-with-php expliquant pourquoi vous ne devriez pas faire cela – Vika

+0

duplication possible de [crawling une page html en utilisant php?] (http://stackoverflow.com/questions/3946506/crawling-a-html-page-using-php) – Gordon

+0

* (apparenté) * [Meilleures méthodes pour analyser HTML] (http://stackoverflow.com/ questions/3577641/best-methods-to-parse-html/3577662 # 3577662) – Gordon

Répondre

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

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

print $p1; 

Ceci ne marchera que si vos balises P sont < p> content. </p> Dès que vous commencez à ajouter des attributs, il va se casser. Quelle est l'une des raisons pour lesquelles vous ne devriez pas utiliser regex pour extraire du HTML en premier lieu.

+0

. +? <\/p> ne se cassera pas si p a des attributs –

2

Eh bien, vous pouvez le faire avec RegExp mais ce n'est pas conseillé. Référez-vous Parsing Html The Cthulhu Way

Je peux prévoir que vous pourriez chercher du HTML (ou lire à partir d'un fichier) et que vous voulez en extraire du texte. HTML est pour la mise en page, pas un langage structuré pour le stockage de données.