CFLib.org – Common Function Library Project

stringToAscii(str)

Last updated June 17, 2010

Version: 1 | Requires: ColdFusion MX | Library: CFMLLib

 
Rated 2 time(s). Average Rating: 5.0

Description:
Pass me a string and I'll convert it into HTML-friendly ASCII characters.

Return Values:
Returns a string.

Example:

view plain print about
<cfset str = "Ive ¼ got © some " funky ¬ characters ? to ? convert ¥ into ® ASCII ¶ eh?" />
<cfdump var="
#stringToAscii(str)#" />

Parameters:

Name Description Required
str String to parse. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * I convert a string to ASCII characters.
 * 
 * @param str      String to parse. (Required)
 * @return Returns a string. 
 * @author Stephen Withington (steve@stephenwithington.com) 
 * @version 1, June 17, 2010 
 */

function stringToAscii(str) {
    var local = StructNew();
    local.oldStr = '';
    local.newStr = '';
    if ( StructKeyExists(arguments, 'str') and IsSimpleValue(arguments.str) ) {
        local.oldStr = arguments.str;
        for ( local.i=1; local.i lte Len(arguments.str); local.i++ ) {
            local.newStr = local.newStr & '&##' & Asc(Left(local.oldStr,1)) & ';';
            local.oldStr = RemoveChars(local.oldStr,1,1);
        };
    };
    return local.newStr;
};
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Robby Lansaw Robby Lansaw added
getComponentProp...
3 day(s) ago

Dave Anderson Dave Anderson added
iniToStruct
25 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
25 day(s) ago

Richard Richard added
dice
27 day(s) ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson