SunQuest
 
       ASP
  Home arrow ASP arrow Page 4 - Mimicking PHP's String Formatting Function...
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
     
    Iron Speed
     
    ADVERTISEMENT

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    Mimicking PHP's String Formatting Functions - str_pad


    (Page 4 of 4 )

    PHP has any interesting little function called str_pad. You basically hand any string to the function with the desired length, and it appends spaces to the string until reaching that length. You have the options of specifying the characters to append instead of spaces, and also where to pad (left, right, or both). This would be handy for padding numbers with leading zeros, and perhaps you have other uses.

    Unfortunately, ASP does not allow for optional parameters. So if you want to use the default padding of a space, and the default location of padding to the right, just throw in empty quotations as parameters.

    There are two functions referenced in the code that may be slightly foreign to you, (isTrulyNumeric & errorMessage) but you can find them explained at: http://www.aspfree.com/c/a/ASP/Easy-Error-Management/

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

    Function strPad( strToPad, toLength, padWith, padWhere )

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

    'padWhere: 1=right, 2=left, 3=both

    strToPad = strToPad & ""

    dim padBoth

    padBoth = false

    if not isTrulyNumeric( toLength ) then

    call errorMessage( "Desired length not specified!")

    else

    toLength = cint( toLength )

    end if

    if padWith = "" then padWith = " "

    if not isReallyNumeric( padWhere ) then

    padWhere = 1

    else

    padWhere = cInt( padWhere )

    if padWhere > 3 or padWhere < 1 then padWhere = 1

    if padWhere = 3 then

    padBoth = true

    padWhere = 1

    end if

    end if

    do until len( strtoPad ) >= toLength

    select case padWhere

    case 1

    strtoPad = strtoPad & padWith

    case 2

    strtoPad = padWith & strtoPad

    end select

    if padBoth then

    if padWhere = 1 then

    padWhere = 2

    else

    padWhere = 1

    end if

    end if

    loop

    if len( strToPad ) > toLength then

    if ( padBoth and padWhere = 1 ) or ( padWhere = 2 and not padBoth ) then '=== no carriage return here

    strToPad = right( strToPad, toLength )

    else

    strToPad = left( strToPad, toLength )

    end if

    end if

    strPad = strToPad

    End Function

    So you can see that if it's specified to pad on both left and right, the do loop simply alternates between left and right. At the end of it all, if the resulting string is longer than the desired length, it's trimmed in the proper location.

    Therefore, typing in:

    response.write( strPad( "dog", 10, "CAT", "" ) )

    will output:

    dogCATCATC

    Conclusion

    I hope this article has taught you a very important lesson, ALWAYS look both ways before crossing the road! No wait, wrong lesson. I meant to say, ALWAYS look for a creative and reusable solution to your problem, and if you see something prepackaged in another language or environment, why not make your own version of it?

    You should be able to find lots of uses for these functions, but I also hope you find success in creating your own custom (and maybe borrowed) functions as well. Like I mentioned before, feel free to share them in the forum!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    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 3 hosted by Hostway