CFLib.org – Common Function Library Project

GetTagContent(tag, string)

Last updated September 16, 2002

author

Johan Steenkamp

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Returns the contents enclosed in a tag pair if found tag pair is found in the string. If the tag is not found the UDF will return an empty string. Uses two regular expressions to find start and end tag postions and then extract content. Single regular expression solutions that use a subexpression, typically (.^), to extract content do not always work.

Return Values:
Returns a string.

Example:

<cfhttp method="get" url="http://www.hp.com" resolveurl="yes">
<cfset importedContent = cfhttp.fileContent>

<cfoutput>
Title = #HTMLEditFormat(getTagContent("title",importedContent))#
</cfoutput>

Parameters:

Name Description Required
tag The tag to look for. Should be passed without < or > and without attributes. Yes
string The string to search. Yes

Full UDF Source:

/**
 * Returns the content enclosed in a tag pair.
 * 
 * @param tag 	 The tag to look for. Should be passed without < or > and without attributes. (Required)
 * @param string 	 The string to search. (Required)
 * @return Returns a string. 
 * @author Johan Steenkamp (johan@orbital.co.nz) 
 * @version 1, September 16, 2002 
 */
function getTagContent(tag,str) {
	var matchStruct = structNew();
	var startTag = "<#tag#[^>]*>";
	var endTag = "</#tag#>";
	var endTagStart = 0;
	matchStruct = REFindNoCase(startTag,str,1,"true");
	if(matchStruct.len[1] eq 0 ) return ""; 
	endTagStart = REFindNoCase(endTag,str,matchStruct.pos[1],"false");
	return Mid(str,matchStruct.pos[1]+matchStruct.len[1],endTagStart-matchStruct.pos[1]-matchStruct.len[1]);
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson