home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / wordcount.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  2.5 KB  |  85 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. Dim strScriptName
  15. Dim strInputText
  16.  
  17. ' Read in the script name so I know where to
  18. ' point the form's action to.
  19. strScriptName = Request.ServerVariables("URL")
  20.  
  21. ' Read in the input from the text area.
  22. strInputText = Request.Form("txtWordCount")
  23.  
  24. ' Check for empty input and ignore it...
  25. If strInputText = "" Then
  26.     strInputText = "This is some text for the textarea!"
  27. Else
  28.     ' Echo out the input:
  29.     Response.Write "You entered:<br />" & vbCrLf
  30.     Response.Write "<pre>"
  31.     Response.Write Server.HTMLEncode(strInputText)
  32.     Response.Write "</pre>" & vbCrLf
  33.  
  34.     ' Print out the counts we got:
  35.     Response.Write "<p>It contained <b>" _
  36.         & GetWordCount(strInputText) _
  37.         & "</b> words and <b>" _
  38.         & GetCharCount(strInputText) _
  39.         & "</b> characters.</p><br />" & vbCrLf
  40. End If
  41.  
  42. ' I wrapped these into functions so you can reuse them.
  43. '**** Begin Functions ***********************************
  44. Function GetWordCount(strInput)
  45.     Dim strTemp
  46.  
  47.     ' Deal with tabs and carriage returns
  48.     ' by replacing them with spaces.
  49.     strTemp = Replace(strInput, vbTab, " ")
  50.     strTemp = Replace(strTemp, vbCr, " ")
  51.     strTemp = Replace(strTemp, vbLf, " ")
  52.  
  53.     ' Remove leading and trailing spaces
  54.     strTemp = Trim(strTemp)
  55.  
  56.     ' Combine multiple spaces down to single ones
  57.     Do While InStr(1, strTemp, "  ", 1) <> 0
  58.         strTemp = Replace(strTemp, "  ", " ")
  59.     Loop
  60.  
  61.     ' Get a count by splitting the string into an array
  62.     ' and retreiving the number of elements in it.
  63.     ' I add one to deal with the 0 lower bound.
  64.     GetWordCount = UBound(Split(strTemp, " ", -1, 1)) + 1
  65. End Function ' GetWordCount
  66.  
  67. Function GetCharCount(strInput)
  68.     GetCharCount = Len(strInputText)
  69. End Function ' GetCharCount
  70. '**** End Functions *************************************
  71.  
  72. ' Here's our form that we fill with the value they
  73. ' entered last time.
  74. %>
  75. <p>Enter some text:</p>
  76.  
  77. <form action="<%= strScriptName %>" method="post">
  78.     <textarea name="txtWordCount" cols="40" rows="5"
  79.     ><%= Server.HTMLEncode(strInputText) %></textarea>
  80.  
  81.     <br />
  82.  
  83.     <input type="submit">
  84. </form>
  85.