php把格式化的字串寫入一個變數中函數sprintf()

來源:互聯網
上載者:User

執行個體

把百分比符號(%)符號替換成一個作為參數進行傳遞的變數:

<?php$number = 9;$str = "Beijing";$txt = sprintf("There are %u million bicycles in %s.",$number,$str);echo $txt;?>

定義和用法

sprintf() 函數把格式化的字串寫入一個變數中。

arg1、arg2、++ 參數將被插入到主字串中的百分比符號(%)符號處。該函數是逐步執行的。在第一個 % 符號處,插入 arg1,在第二個 % 符號處,插入 arg2,依此類推。

注釋:如果 % 符號多於 arg 參數,則您必須使用預留位置。預留位置被插入到 % 符號之後,由數字和 "\$" 組成。請參見執行個體 2。

提示:相關函數:printf()、 vprintf()、 vsprintf()、 fprintf() 和 vfprintf()

文法

sprintf(format,arg1,arg2,arg++)
參數 描述
format 必需。規定字串以及如何格式化其中的變數。

可能的格式值:

  • %% - 返回一個百分比符號 %

  • %b - 位元

  • %c - ASCII 值對應的字元

  • %d - 包含加號或減號的十進位數(負數、0、正數)

  • %e - 使用小寫科學計數法(例如 1.2e+2)

  • %E - 使用大寫的科學計數法(例如 1.2E+2)

  • %u - 不包含加號或減號的十進位數(大於等於 0)

  • %f - 浮點數(本地設定)

  • %F - 浮點數(非本地設定)

  • %g - 較短的 %e 和 %f

  • %G - 較短的 %E 和 %f

  • %o - 八位元

  • %s - 字串

  • %x - 十六進位數(小寫字母)

  • %X - 十六進位數(大寫字母)

附加的格式值。必需放置在 % 和字母之間(例如 %.2f):

  • + (在數字前面加上 + 或 - 來定義數位正負性。預設情況下,只有負數才做標記,正數不做標記)

  • ' (規定使用什麼作為填充,預設是空格。它必須與寬度指定器一起使用。例如:%'x20s(使用 "x" 作為填充))

  • - (左調整變數值)

  • [0-9] (規定變數值的最小寬度)

  • .[0-9] (規定小數位元或最大字串長度)

注釋:如果使用多個上述的格式值,它們必須按照上面的順序進行使用,不能打亂。

arg1 必需。規定插到 format 字串中第一個 % 符號處的參數。
arg2 可選。規定插到 format 字串中第二個 % 符號處的參數。
arg++ 可選。規定插到 format 字串中第三、四等等 % 符號處的參數。

技術細節

傳回值: 返回已格式化的字串。
PHP 版本: 4+

更多執行個體

執行個體 1

使用格式值 %f:

<?php$number = 123;$txt = sprintf("%f",$number);echo $txt;?>

執行個體 2

使用預留位置:

<?php$number = 123;$txt = sprintf("With 2 decimals: %1$.2f<br>With no decimals: %1$u",$number);echo $txt;?>

執行個體 3

所有可能的格式值的示範:

<?php$num1 = 123456789;$num2 = -123456789;$char = 50; // The ASCII Character 50 is 2// Note: The format value "%%" returns a percent signecho sprintf("%%b = %b",$num1)."<br>"; // Binary numberecho sprintf("%%c = %c",$char)."<br>"; // The ASCII Characterecho sprintf("%%d = %d",$num1)."<br>"; // Signed decimal numberecho sprintf("%%d = %d",$num2)."<br>"; // Signed decimal numberecho sprintf("%%e = %e",$num1)."<br>"; // Scientific notation (lowercase)echo sprintf("%%E = %E",$num1)."<br>"; // Scientific notation (uppercase)echo sprintf("%%u = %u",$num1)."<br>"; // Unsigned decimal number (positive)echo sprintf("%%u = %u",$num2)."<br>"; // Unsigned decimal number (negative)echo sprintf("%%f = %f",$num1)."<br>"; // Floating-point number (local settings aware)echo sprintf("%%F = %F",$num1)."<br>"; // Floating-point number (not local sett aware)echo sprintf("%%g = %g",$num1)."<br>"; // Shorter of %e and %fecho sprintf("%%G = %G",$num1)."<br>"; // Shorter of %E and %fecho sprintf("%%o = %o",$num1)."<br>"; // Octal numberecho sprintf("%%s = %s",$num1)."<br>"; // Stringecho sprintf("%%x = %x",$num1)."<br>"; // Hexadecimal number (lowercase)echo sprintf("%%X = %X",$num1)."<br>"; // Hexadecimal number (uppercase)echo sprintf("%%+d = %+d",$num1)."<br>"; // Sign specifier (positive)echo sprintf("%%+d = %+d",$num2)."<br>"; // Sign specifier (negative)?>

先舉個最簡單的案例

<?php$str1="1234";echo sprintf("hello%s","$str1");//效果為: hello1234?>

這什麼意思呢

要點:

%s = %符號和後面屬性符號(s)總稱為插入標記組合,也就是把後面準備進行格式化的值($str1)替換在這個位置

hello = 這個單詞就是很多人蒙蔽的地方,告訴你這個什麼代表也沒有,就單純的代表一個hello,用於分割或者修飾用,一般用[ %s ]、<%s>這樣格式化出來後就直接在標籤裡

記住,一個%標記符後面只有一個類型屬性(比如s),s是什麼上面有,以字串的方式格式化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.