– Common Function Library Project

GetTagContent(tag, string)

Last updated September 16, 2002


Johan Steenkamp

Version: 1 | Requires: CF5 | Library: StrLib

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.


<cfhttp method="get" url="" resolveurl="yes">
<cfset importedContent = cfhttp.fileContent>

Title = #HTMLEditFormat(getTagContent("title",importedContent))#


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 ( 
 * @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


Latest Additions

Kevin Cotton added
May 5, 2016

Raymond Camden added
April 25, 2016

Chris Wigginton added
January 18, 2016

Gary Stanton added
November 19, 2015

Sebastiaan Naafs - van Dijk added
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson