บทช่วยสอนPHP

PHP HOME แนะนำ PHP การติดตั้ง PHP PHP ไวยากรณ์ ความคิดเห็น PHP ตัวแปร PHP PHP Echo / พิมพ์ ประเภทข้อมูล PHP PHP Strings หมายเลข PHP PHP คณิตศาสตร์ ค่าคงที่ PHP ตัวดำเนินการ PHP PHP If...Else...Elseif PHP Switch PHP Loops ฟังก์ชัน PHP PHP Arrays PHP Superglobals PHP RegEx

แบบฟอร์มPHP

การจัดการแบบฟอร์ม PHP การตรวจสอบแบบฟอร์ม PHP ต้องใช้แบบฟอร์ม PHP URL แบบฟอร์ม PHP/อีเมล แบบฟอร์ม PHP เสร็จสมบูรณ์

PHPขั้นสูง

วันที่และเวลา PHP รวม PHP การจัดการไฟล์ PHP ไฟล์ PHP เปิด/อ่าน สร้าง/เขียนไฟล์ PHP อัพโหลดไฟล์ PHP คุกกี้ PHP PHP Sessions ตัวกรอง PHP ตัวกรอง PHP ขั้นสูง ฟังก์ชันเรียกกลับของ PHP PHP JSON ข้อยกเว้น PHP

PHP OOP

PHP OOP คืออะไร คลาส PHP/วัตถุ ตัวสร้าง PHP PHP Destructor PHP Access Modifiers PHP Inheritance ค่าคงที่ PHP PHP Abstract Classes อินเทอร์เฟซ PHP PHP ลักษณะ PHP Static Methods PHP คุณสมบัติคงที่ PHP เนมสเปซ PHP Iterables

ฐานข้อมูลMySQL

ฐานข้อมูล MySQL MySQL Connect MySQL สร้าง DB MySQL สร้างตาราง MySQL แทรกข้อมูล MySQL รับ ID ล่าสุด MySQL แทรกหลายรายการ MySQL ที่เตรียมไว้ MySQL Select Data MySQL Where MySQL สั่งซื้อโดย MySQL ลบข้อมูล ข้อมูลอัพเดต MySQL MySQL Limit Data

PHP XML

PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - รับ PHP XML Expat PHP XML DOM

PHP - AJAX

บทนำ AJAX AJAX PHP ฐานข้อมูล AJAX AJAX XML AJAX ค้นหาสด AJAX โพล

ตัวอย่างPHP

ตัวอย่าง PHP PHP คอมไพเลอร์ แบบทดสอบ PHP แบบฝึกหัด PHP ใบรับรอง PHP

ข้อมูลอ้างอิงPHP

ภาพรวม PHP PHP Array ปฏิทิน PHP PHP วันที่ ไดเรกทอรี PHP ข้อผิดพลาด PHP ข้อยกเว้น PHP ระบบไฟล์ PHP ตัวกรอง PHP PHP FTP PHP JSON คีย์เวิร์ด PHP PHP Libxml PHP Mail PHP คณิตศาสตร์ PHP เบ็ดเตล็ด PHP MySQLi เครือข่าย PHP การควบคุมเอาต์พุต PHP PHP RegEx PHP SimpleXML PHP Stream PHP String
addcslashes() เครื่องหมายทับ () bin2hex() สับ() chr() chunk_split() Convert_cyr_string() convert_uudecode() Convert_uuencode() count_chars() crc32() ห้องใต้ดิน() เสียงสะท้อน() ระเบิด() พิมพ์ () get_html_translation_table() ฮีบรู () ภาษาฮีบรูฟ () hex2bin() html_entity_decode() htmlentities() htmlspecialchars_decode() htmlspecialchars() ระเบิด() เข้าร่วม() lcfirst() เลเวนชไทน์() localeconv() ลทริม () md5() md5_file() เมตาโฟน() money_format() nl_langinfo () nl2br() number_format() คำ() parse_str() พิมพ์() พิมพ์f() quoted_printable_decode() quoted_printable_encode() ข้อมูลอ้างอิง () อาร์ทริม() setlocale() sha1() sha1_file() คล้ายกัน_text() ซาวด์เอ็กซ์() sprintf() sscanf() str_getcsv () str_ireplace () str_pad() str_repeat() str_replace() str_rot13 () str_shuffle() str_split () str_word_count() strcasecmp() strchr() strcmp() strcoll() strcspn() strip_tags() แถบสแลช() แถบสแลช() การ์ตูน () แถบ () strlen() strnatcasecmp() strnatcmp() strncasecmp() strncmp() strpbrk () สตรอปส์() strchr() strrev () สตริพอส() สตริพอส() strspn () strstr() strtok() strtolower() สตรอปเปอร์() strtr() ย่อย () substr_compare() substr_count() substr_replace() ตัดแต่ง() ucfirst() ucwords() vfprintf() vprintf() vsprintf() ตัดคำ()
การจัดการตัวแปร PHP PHP XML Parser PHP Zip เขตเวลา PHP

PHP vsprintf()ฟังก์ชัน

❮ การอ้างอิงสตริง PHP

ตัวอย่าง

เขียนสตริงที่จัดรูปแบบให้กับตัวแปร:

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

ความหมายและการใช้งาน

ฟังก์ชัน vsprintf() เขียนสตริงที่จัดรูปแบบไปยังตัวแปร

อาร์กิวเมนต์ใน vsprintf() ต่างจาก sprintf() ในอาร์เรย์ อิลิเมนต์อาร์เรย์จะถูกแทรกที่เครื่องหมายเปอร์เซ็นต์ (%) ในสตริงหลัก ฟังก์ชันนี้ใช้งานได้ "ทีละขั้นตอน" ที่สัญญาณ % แรก องค์ประกอบอาร์เรย์แรกจะถูกแทรก ที่เครื่องหมาย % ที่สอง องค์ประกอบอาร์เรย์ที่สองจะถูกแทรก เป็นต้น

หมายเหตุ:หากมีเครื่องหมาย % มากกว่าอาร์กิวเมนต์ คุณต้องใช้ตัวยึดตำแหน่ง ตัวยึดตำแหน่งจะถูกแทรกหลังเครื่องหมาย % และประกอบด้วยหมายเลขอาร์กิวเมนต์และ "\$" ดูตัวอย่างที่สอง

เคล็ดลับ:ฟังก์ชั่นที่เกี่ยวข้อง: fprintf() , vfprintf() , printf() , sprintf()และvprintf( )


ไวยากรณ์

vsprintf(format,argarray)

ค่าพารามิเตอร์

Parameter Description
format Required. Specifies the string and how to format the variables in it.

Possible format values:

  • %% - Returns a percent sign
  • %b - Binary number
  • %c - The character according to the ASCII value
  • %d - Signed decimal number (negative, zero or positive)
  • %e - Scientific notation using a lowercase (e.g. 1.2e+2)
  • %E - Scientific notation using a uppercase (e.g. 1.2E+2)
  • %u - Unsigned decimal number (equal to or greather than zero)
  • %f - Floating-point number (local settings aware)
  • %F - Floating-point number (not local settings aware)
  • %g - shorter of %e and %f
  • %G - shorter of %E and %f
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)

Additional format values. These are placed between the % and the letter (example %.2f):

  • + (Forces both + and - in front of numbers. By default, only negative numbers are marked)
  • ' (Specifies what to use as padding. Default is space. Must be used together with the width specifier. Example: %'x20s (this uses "x" as padding)
  • - (Left-justifies the variable value)
  • [0-9] (Specifies the minimum width held of to the variable value)
  • .[0-9] (Specifies the number of decimal digits or maximum string length)

Note: If multiple additional format values are used, they must be in the same order as above.

argarray Required. An array with arguments to be inserted at the % signs in the format string


รายละเอียดทางเทคนิค

คืนมูลค่า: ส่งกลับค่าอาร์เรย์เป็นสตริงที่จัดรูปแบบ
รุ่น PHP: 4.1.0+

ตัวอย่างเพิ่มเติม

ตัวอย่าง

การใช้ค่ารูปแบบ %f:

<?php
$num1 = 123;
$num2 = 456;
$txt = vsprintf("%f%f",array($num1,$num2));
echo $txt;
?>

ตัวอย่าง

การใช้ตัวยึดตำแหน่ง:

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

ตัวอย่าง

ใช้ sprintf() เพื่อแสดงค่ารูปแบบที่เป็นไปได้ทั้งหมด:

<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // The ASCII Character 50 is 2

// Note: The format value "%%" returns a percent sign
echo sprintf("%%b = %b",$num1)."<br>"; // Binary number
echo sprintf("%%c = %c",$char)."<br>"; // The ASCII Character
echo sprintf("%%d = %d",$num1)."<br>"; // Signed decimal number
echo sprintf("%%d = %d",$num2)."<br>"; // Signed decimal number
echo 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 %f
echo sprintf("%%G = %G",$num1)."<br>"; // Shorter of %E and %f
echo sprintf("%%o = %o",$num1)."<br>"; // Octal number
echo sprintf("%%s = %s",$num1)."<br>"; // String
echo 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 = "Hello";
$str2 = "Hello world!";

echo vsprintf("[%s]",array($str1))."<br>";
echo vsprintf("[%8s]",array($str1))."<br>";
echo vsprintf("[%-8s]",array($str1))."<br>";
echo vsprintf("[%08s]",array($str1))."<br>";
echo vsprintf("[%'*8s]",array($str1))."<br>";
echo vsprintf("[%8.8s]",array($str2))."<br>";
?>

❮ การอ้างอิงสตริง PHP