– 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]);


Latest Additions

Raymond Camden added
November 04, 2017

Leigh added
May 11, 2016

Raymond Camden added
May 10, 2016

Kevin Cotton added
May 05, 2016

Raymond Camden added
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson