CFLib.org – Common Function Library Project

GetHTMLTitle(str)

Last updated December 03, 2001

author

Raymond Camden

Version: 1 | Requires: CF5 | Library: StrLib

Description:
This UDF will parse an HTML string (normally returned from CFFILE) and return the text between the title tags. The UDF will return an empty string if it cannot find proper HTML title tags.

Return Values:
Returns a string.

Example:

<CFOUTPUT>
<CFSAVECONTENT VARIABLE="FakeHTMLFile">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>Interesting Title</title>
</head>

<body>

Hello every

</body>
</html>
</CFSAVECONTENT>

<CFSET Title = GetHTMLTitle(FakeHTMLFile)>
The title of this html file is #Title#
</CFOUTPUT>

Parameters:

Name Description Required
str The HTML string to check. Yes

Full UDF Source:

/**
 * Parses an HTML page and returns the title.
 * 
 * @param str      The HTML string to check. 
 * @return Returns a string. 
 * @author Raymond Camden (ray@camdenfamily.com) 
 * @version 1, December 3, 2001 
 */
function GetHTMLTitle(str) {
    var matchStruct = reFindNoCase("<[[:space:]]*title[[:space:]]*>([^<]*)<[[:space:]]*/title[[:space:]]*>",str,1,1);
    if(arrayLen(matchStruct.len) lt 2) { writeoutput("error<BR>");return ""; }
    return Mid(str,matchStruct.pos[2],matchStruct.len[2]);    
}

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