SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Saturday, May 23, 2009

Strip html tags

This article deals with how to strip the html tags and fetch the tags we needed, after referring some of the articles (dzone.snippets) and with the help of my friend Jon

Here we have used regex with grep method.Pattern for matching the tags /<p>(.+?)</p>/
For example i have used for p tag

text="<p>He's the man who helped make \"Slumdog Millionaire\" an international hit, scoring the soundtrack of the Oscar winning film. Despite his performance at the Oscars ceremony and being caught up in all the glitz and adulation, Rahman is a reluctant star.</p>\r\n<p>He's worked on films since he was a teenager, taking over the role of family breadwinner after his father died and followed in his footsteps as a composer.</p>\r\n<p>While he had stints writing advertising jingles in India, composing for films as been his life's work so far, yet from his studio in Chennai he admitted to CNN he didn't want to score films.</p>\r\n<h3>El pasaje est&aacute;ndar Lorem Ipsum, usado desde el a&ntilde;o 1500.</h3>\r\n"

Fetch the first p tag


text.grep(/<p>(.+?)<\/p>/).first

"<p>He's the man who helped make \"Slumdog Millionaire\" an international hit, scoring the soundtrack of the Oscar winning film. Despite his performance at the Oscars ceremony and being caught up in all the glitz and adulation, Rahman is a reluctant star.</p>\r\n"

Fetch the last p tag


text.grep(/<p>(.+?)<\/p>/).last

"<p>While he had stints writing advertising jingles in India, composing for films as been his life's work so far, yet from his studio in Chennai he admitted to CNN he didn't want to score films.</p>\r\n"