บทช่วยสอน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 sscanf()ฟังก์ชั่น

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

ตัวอย่าง

แยกสตริง:

<?php
$str = "age:30 weight:60kg";
sscanf($str,"age:%d weight:%dkg",$age,$weight);
// show types and values
var_dump($age,$weight);
?>

ฟังก์ชัน sscanf() จะแยกวิเคราะห์อินพุตจากสตริงตามรูปแบบที่ระบุ ฟังก์ชัน sscanf() จะแยกสตริงออกเป็นตัวแปรตามรูปแบบสตริง

หากส่งผ่านพารามิเตอร์เพียงสองพารามิเตอร์ไปยังฟังก์ชันนี้ ข้อมูลจะถูกส่งกลับเป็นอาร์เรย์ มิฉะนั้น หากส่งผ่านพารามิเตอร์ทางเลือก ข้อมูลที่แยกวิเคราะห์จะถูกเก็บไว้ในพารามิเตอร์เหล่านั้น หากมีตัวระบุมากกว่าตัวแปรที่จะบรรจุไว้ ข้อผิดพลาดจะเกิดขึ้น อย่างไรก็ตาม หากมีตัวระบุน้อยกว่าตัวแปร ตัวแปรพิเศษจะมีค่า NULL

ฟังก์ชั่นที่เกี่ยวข้อง:

  • printf() - ส่งออกสตริงที่จัดรูปแบบ
  • sprintf() - เขียนสตริงที่จัดรูปแบบไปยังตัวแปร

ไวยากรณ์

sscanf(string,format,arg1,arg2,arg++)

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

Parameter Description
string Required. Specifies the string to read
format Required. Specifies the format to use.

Possible format values:

  • %% - Returns a percent sign
  • %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)
  • %u - Unsigned decimal number (equal to or greather than zero)
  • %f - Floating-point number
  • %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.

arg1 Optional. The first variable to store data in
arg2 Optional. The second variable to store data in
arg++ Optional. The third, fourth, and so on, to store data in


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

คืนมูลค่า: หากส่งผ่านพารามิเตอร์เพียงสองพารามิเตอร์ไปยังฟังก์ชันนี้ ข้อมูลจะถูกส่งกลับเป็นอาร์เรย์ มิฉะนั้น หากส่งผ่านพารามิเตอร์ทางเลือก ข้อมูลที่แยกวิเคราะห์จะถูกเก็บไว้ในพารามิเตอร์เหล่านั้น หากมีตัวระบุมากกว่าตัวแปรที่จะบรรจุไว้ ข้อผิดพลาดจะเกิดขึ้น อย่างไรก็ตาม หากมีตัวระบุน้อยกว่าตัวแปร ตัวแปรพิเศษจะมีค่า NULL
รุ่น PHP: 4.0.1+

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

ตัวอย่าง

การใช้ค่ารูปแบบ %s, %d และ %c:

<?php
$str = "If you divide 4 by 2 you'll get 2";
$format = sscanf($str,"%s %s %s %d %s %d %s %s %c");
print_r($format);
?>

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