Find First Paragraph
Today I had the need to create a script that would find the first paragraph in a string and use this as the extract for a news article.
So, I thought I’d share the ColdFusion code with everyone:
<cfsavecontent variable="paragraphs">
<p>This is the first paragraph.</p>
<p>Here's the second paragraph.</p>
<p>And finally, the third paragraph.</p>
</cfsavecontent>
<cfset request.firstParagraph = findNoCase("</p>",trim(paragraphs)) />
<cfif request.firstParagraph gt 0>
<cfset request.firstParagraph = request.firstParagraph+3 />
<cfset request.newsExtract = left(trim(paragraphs),request.firstParagraph) />
<cfelse>
<cfset request.newsExtract = trim(paragraphs) />
</cfif>
<textarea id="newsExtract" name="newsExtract" cols="40" rows="5">
<cfoutput>#request.newsExtract#</cfoutput>
</textarea>
I hope that others find this useful.
Update: 07 December 2006
I have now turned this into a nifty function:
<cfscript>
function firstParagraph(str)
{
str = trim(str);
endTag = findNoCase("</p>", str);
if (endTag gt 0)
{
endTag = endTag + 3;
extract = left(str, endTag);
}
else
{
extract = str;
}
return extract;
}
</cfscript>
This should make using the code even easier.
Update: 08 December 2006
This function is now available on CFLib.org!
About
Neil Merton is a South African born web professional currently employed by the Servelogic. These are his thoughts and memories.
Recent Posts
- Twitter Updates for 2008-02-06
- Twitter Updates for 2008-02-03
- Twitter Updates for 2008-02-02
- Website Updated to 2.3.2
- Web2.0* Version 1.7.0 Available
- Web2.0* Theme Gets It’s Own Website
- Happy Festive Greetings
- Web2.0* Version 1.6.0 Available
- Web2.0* and Wordpress 2.3
- Wordpress Unable to use .htaccess
Categories
Archives
- February 2008
- January 2008
- December 2007
- October 2007
- August 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
Sorry comments are closed for this entry.