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

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson