Replacing # character in user input strings

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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:...
    3. 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...
    4. 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...
    5. [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...
  3. #2

    Default Re: Replacing # character in user input strings

    Do you have any reply on this. I have a similar problem
    Hanno1962 Guest

  4. #3

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139