ASP
  Home arrow ASP arrow Page 2 - Mimicking PHP's String Formatting Function...
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

Mimicking PHP's String Formatting Functions
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2004-06-21

    Table of Contents:
  • Mimicking PHP's String Formatting Functions
  • str_word_count and ucfirst
  • strip_tags
  • str_pad

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Mimicking PHP's String Formatting Functions - str_word_count and ucfirst


    (Page 2 of 4 )

    Let me explain the str_word_count function: It counts words.

    Yes, simple as that, you feed it a string, and it feeds you back the number of separate words within the string. Here's how we could duplicate it in ASP:

    '==================================================

    Function strWordCount( strToCount )

    '==================================================

    Dim arWords


    strToCount = Trim( strToCount & "" )

    arWords = split( strToCount, " " )

    strWordCount = uBound( arWords ) + 1


    End Function

    Yes, it is really very simple. It just breaks the string by its spaces, as spaces logically define the separation of words. These are broken into an array, and the array is counted to see how many words it's holding. Of course we need to add one to the number, as arrays are zero-based, and we're not.

    This will even work if you hand it a string with no spaces. It will not break, the array will contain the entire string as the first item of the array, and the word count will be one. Also, the first line of the function prevents any runtime errors due to handing the function a null value, and it does this by turning it into a string.

    So that was simple, let's move on to the next two.

    Sometimes it is very important to have proper formatting in a string. One example is when a user enters their City into a form, you may want to match it to a city on your database. It is essential to ensure that you establish and enforce proper formatting. In most cases it would be to have the first letter capitalized, and the rest lowercase. Yes, we can prevent problems caused by the angry people who type in all caps, or the lazy ones like myself who never hit the shift key!

    PHP has a great little function call uc_first, which returns a string formatted exactly as I described. Here's a simple way to do exactly the same thing with ASP.

    '===================================================

    Function ucFirst( strWord )

    '===================================================

    strWord = trim( strWord & "" )

    if len( strWord ) > 0 then

    ucFirst = uCase( left( strWord, 1 ) ) & _

    lcase( right( strWord, len( strWord ) - 1 ) )

    end if

    End Function

    Going a step further, it was realized that you may have a string containing many words, and you want them ALL to be formatted in this fashion. A good example of this is a title. No matter how a title has been entered, you still want it to look like a title, and so we shall:

    '===================================================

    Function ucWords( strWords )

    '===================================================

    strWords = trim( strWords & "" )

    if len( strWords ) > 0 then

    dim arWords, i, strFormatted

    arWords = split( strWords, " " )

    for i = 0 to uBound( arWords )

    strFormatted = strFormatted & " " & ucFirst( arWords( i ) )

    next

    ucWords = strFormatted

    end if

    End Function

    So if you handed this function a string like this:

    response.write( "i dON'T liKe thiS ARTICLE At all" )

    It would come back formatted properly, and the output should be:

    "I Love This Article"

    Hmm, wishful thinking... if only my functions were that smart! Well then, let's move on.

    More ASP Articles
    More By Justin Cook


     

    ASP ARTICLES

    - ADO for the Beginner
    - ADO.NET 101: Data Rendering with a DataGrid ...
    - Introducing SoftArtisans OfficeWriter 3.0 En...
    - Getting Remote Files With ASP
    - The Real Basics of Functions in ASP
    - Enhancing Readability with ASP
    - Mimicking PHP's String Formatting Functions
    - Windows Server Hacks 12, 77, and 98
    - How to Sort a Multi-Dimensional Array
    - Developing an Information Management Tool wi...
    - What are Active Server Pages?
    - Getting Remote Pages with ASP
    - FTP’ing Files with ASP
    - Apply Single-Sign-On to Your Application
    - Easy Error Management





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway