Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.
-
mate of the state #1
Replacing # character in user input strings
I have a custom tag I use quite often for formatting large strings with HTML
paragraph tags as well as some other useful text manipulation like auto-linking
URLs. Recently, I discovered teh tag will cause errors when the supplied
string contains a # character. How can I effectively ignore those characters
and tell ColdFusion not to try to parse them under any circumstances? I've
attached the code for the custom tag. The errors only occur when strings are
passed through this code. Users can enter # characters in other strings which
can be retrieved and displayed no problem. Thanks!
<!--- string to be processed --->
<CFparam name="attributes.string" type="string" default="You must supply
CF_pFormat a string, fool!">
<!--- toggle whether or not to process URLs in text body --->
<CFparam name="attributes.urlFormat" default="yes">
<!--- set how many words to show as a excerpt of the full text --->
<CFparam name="attributes.excerpt" type="numeric" default="0">
<!--- debug setting, if true will apply some special formatting, output, etc.
to help debug what's happening --->
<CFparam name="attributes.debug" default="off">
<!--- trim the string and set the variable referenced throughout --->
<CFset output = trim(attributes.string)>
<!--- trim for excerpt output if necessary --->
<CFif attributes.excerpt gt 0 and attributes.excerpt lt len(output)>
<!--- find first end of sentence after number of characters specified --->
<CFset sentenceEnd = reFind("[.|?|!]",output,attributes.excerpt)>
<CFif sentenceEnd is 0><CFset sentenceEnd = len(output)></CFif>
<CFset output = left(output,sentenceEnd)>
<CFset output = insert(" ...",output,len(output))>
</CFif>
<!--- url formatting, based on code by Andy Birchall (thanks m8!) --->
<CFif attributes.urlFormat is "yes">
<!--- set the start position for the REFind --->
<cfset currentpos = 1>
<!--- create an array to store all the matched positions and lengths --->
<cfset aryMatches = ArrayNew(2)>
<!--- pattern to match a web address --->
<CFset webRE =
"([a-zA-Z]+://)?(www\.)?[\w]+\.[\w]+([\/\.\w]+)?\??([\w=]+[\w&]+)+">
<!--- loop through the text of the banner trying to match any web addresses
--->
<cfloop condition="currentpos lte len(output)">
<!--- search the banner from current position --->
<cfset strURLMatch = REFindNoCase(webRE,output,currentpos,1)>
<!--- if an anchor is found.. --->
<cfif strURLMatch.pos[1] gt 0>
<!--- extract the text, --->
<cfset matchText = mid(output,strURLMatch.pos[1],strURLMatch.len[1])>
<cfset arraypos = arrayLen(aryMatches)+1>
<!--- store the position, length and text --->
<cfset aryMatches[arraypos][1] = strURLMatch.pos[1]>
<cfset aryMatches[arraypos][2] = strURLMatch.len[1]>
<cfset aryMatches[arraypos][3] = matchText>
<!--- move the current position on to the end of the matched text --->
<cfset currentpos = strURLMatch.pos[1] + strURLMatch.len[1]>
<!--- not found so break out of loop --->
<cfelse>
<cfbreak>
</cfif>
</cfloop>
<!--- step through array in reverse order as will be changing the length of
the original string--->
<cfloop from="#ArrayLen(aryMatches)#" to="1" step="-1" index="i">
<CFset matchedURL = aryMatches[i][3]>
<CFif left(matchedURL,7) neq "http://">
<CFset matchedURL = Insert("http://",matchedURL,0)>
</CFif>
<cfset output = Insert("</a>",output,aryMatches[i][1] + aryMatches[i][2] -
1)>
<CFset output = Insert('<a href="' & matchedURL &
'">',output,aryMatches[i][1] - 1)>
</cfloop>
</CFif>
<!--- pFormat --->
<CFset output = insert("<p>",output,0)>
<!--- normalize line endings to uniform format --->
<CFset output = reReplace(output,"\r\n|\r|\n","#Chr(10)#","ALL")>
<!--- insert </p>, start a new line, and insert <p> whenever two or more
carriage returns are found --->
<CFset output =
reReplace(output,"#Chr(10)##Chr(10)#+","</p>#Chr(10)#<p>","ALL")>
<!--- replace single breaks with <br /> --->
<CFset output = reReplace(output,"#Chr(10)#","<br />#Chr(10)#","ALL")>
<!--- the previous replace adds <br /> after </p>... see replace before it --->
<CFset output = reReplace(output,"</p><br />","</p>","ALL")>
<!--- insert final closing tag --->
<CFset output = Insert("</p>",output,Len(output))>
<!--- debug --->
<CFif attributes.debug is "on">
<!--- show HTML tags --->
<CFset output = Replace(output,"<","&##60;","ALL")>
<CFset output = Replace(output,">","&##62;","ALL")>
</CFif>
<!--- return processed string --->
<CFoutput>#output#</CFoutput>
mate of the state Guest
-
Input Chinese Character
I having problem to insert chinese character from textarea input into MS Access DB, the funny character save into DB? I try out <meta... -
replacing certain words in strings
Hi! What I try to write is a php that simpley loads the url after the "?" and also replaces the links with it's own link. Example:... -
limiting character input into editable fields
Is there any way to limit the types of characters that a user can type into an editable field? For example can the field be limited to only... -
replacing everything between 2 strings
Is there a way to replace everything between 2 given strings if it exists? Say I have: $str = "replace anything and everything in here."; Now... -
[PHP] replacing everything between 2 strings
That doesn't really help much... I think you just replied to the wrong post. That answer really doesn't have anything to do with my question, I... -
Hanno1962 #2
Re: Replacing # character in user input strings
Do you have any reply on this. I have a similar problem
Hanno1962 Guest
-
Stressed_Simon #3
Re: Replacing # character in user input strings
Why don't you use Replace() to delete them or replace them with a double #????
Stressed_Simon Guest



Reply With Quote

