CFLib.org – Common Function Library Project

xmlFormatMid(inString, start, count)

Last updated May 29, 2007

author

Adam Mihlfried

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Combines XmlFormat and Mid, but avoids problem of XMLFormat creating an invalid length string by converting special characters such as ampersands, quotes, and less than/greater than symbols.

Return Values:
Returns a string.

Example:

<CFSET address = xmlFormatMid("5th & Washington's Landing",1,15)>

<CFOUTPUT>#address#</CFOUTPUT>

Parameters:

Name Description Required
inString String to parse. Yes
start Starting position for substring. Yes
count Length of returned string. Yes

Full UDF Source:

/**
 * Properly performs substring functionality on XML-Formatted strings.
 * 
 * @param inString      String to parse. (Required)
 * @param start      Starting position for substring. (Required)
 * @param count      Length of returned string. (Required)
 * @return Returns a string. 
 * @author Adam Mihlfried (adam@emscharts.com) 
 * @version 1, May 29, 2007 
 */
function xmlFormatMid(inString, start, count) {
   
   var lowStr = "";
   var retStr = "";
   var tmpStr = "";
   var reversed = "";
   var revpos = 0;
   var realpos = 0;
   var realendpos = 0;
   //Convert high ascii characters to their low ascii equivalents
   lowStr = Replace(inString, Chr(8211), Chr(45), "ALL");
   lowStr = Replace(lowStr, Chr(8212), Chr(45), "ALL");
   lowStr = Replace(lowStr, Chr(8216), Chr(39), "ALL");
   lowStr = Replace(lowStr, Chr(8217), Chr(39), "ALL");
   lowStr = Replace(lowStr, Chr(8220), Chr(34), "ALL");
   lowStr = Replace(lowStr, Chr(8221), Chr(34), "ALL");
   retStr = XmlFormat(mid(trim(lowStr), start, count));
   //if string is longer than mid intended then XmlFormat converted/added extra chars.
   if (len(retStr) gt count) {
   //find position of last ampersand if too long
  
   reversed = reverse(retStr);
   revpos = find("&", reversed);
  
   //length of string - reversed position (characters from right) 
   realpos = len(retStr) - revpos + 1;
   
   //new tmpStr is the substring of previous mid from 1 to position BEFORE ampersand
   tmpStr = mid(retStr,1,realpos-1); 
  
    realendpos = find(";",retStr,realpos);
    
   //if length of new string is zero, then string began with & and we should include symbol
    //as long as count under length of special symbol
   if (len(tmpStr) eq 0 and count gt realendpos-realpos) 
    retStr =  mid(retStr,1,count);
    //if tmpStr + symbol length less than count, then we can use
   else if ( len(tmpStr) + (realendpos - realpos + 1) lte count)
      retStr = mid(retStr, 1, count);
   else
    retStr = tmpStr;
   }
   
   return trim(retStr);
}

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