フォーラム@nifty | フォーラム・サークル | サイトマップ | ヘルプ |
掲示板 コラム お役立ち イベント リンク   | FHPGサイトマップ | お問い合わせ |
TOP > お役立ち > ASP de PHP > str_pad  

str_pad 関数

Scripting by かっしい (2004/02/11 登録)

 ASP with VBScript で PHP の便利な関数を定義してみる (2)

str_pad()

 文字列を固定長の他の文字列で埋める関数。

関数の説明

 str_pad (strText as String, intLength as integer, strPadText as String, intPadType as Integer) as string

 → PHPにおけるこの関数の説明を見る。

str_pad のソース

<%
Public Const STR_PAD_RIGHT = 1
Public Const STR_PAD_LEFT = 0
Public Const STR_PAD_BOTH = 2

Public Function str_pad(strText,intLength,strPadText,intPadType)
  Dim intTextLen
  Dim intPadLen
  Dim i
  Dim j 'Both時右側用
  Dim k 'Both時左側用

  intTextLen = Len(strText)

  If intTextLen > intLength then
    str_pad = strText
    Exit Function
  End If
  If strPadText = "" then
    strPadText = " "
  End If

  intPadLen = Len(strPadText)

  Select Case intPadType
    Case STR_PAD_LEFT
      For i = intTextLen to intLength-1 Step intPadLen
        strText = strText & strPadText
      Next
    If Len(strText) > intLength then
      strText = Left(strText,intLength)
    End If

    Case STR_PAD_BOTH
      j = int((intLength - intTextLen)/2*-1)*-1
      k = int((intLength - intTextLen)/2)

      '右サイドのPadding処理
      If j/intPadLen > 0 then
        For i = 1 to int(j/intPadLen)
          strText = strText & strPadText
        Next
      End if

      If j mod intPadLen <> 0 then
        strText = strText & Left(strPadText,j mod intPadLen)
      End If

      '左サイドのPadding処理
      If k/intPadLen > 0 then
        For i = 1 to int(k/intPadLen)
          strText = strPadText & strText
        Next
      End if

      If k mod intPadLen <> 0 then
        strText = Right(strPadText,k mod intPadLen) & strText
      End If

    Case Else
      For i = intTextLen to intLength-1 Step intPadLen
        strText = strPadText & strText
      Next

      If Len(strText) > intLength then
        strText = Right(strText,intLength)
      End If

  End Select

  str_pad = strText

End Function

%>
ページのトップへ
個人情報保護ポリシー
Copyright© NIFTY 2006 All Rights Reserved.
Copyright© FHPG 2006 All Rights Reserved.