PHP 字符串 sscanf() 函數
sscanf() 是預定義的 PHP 函數。愛掏網 - it200.com它用于根據格式從字符串中解析輸入。愛掏網 - it200.com如果我們在函數中傳遞兩個參數,則數據將作為數組返回。愛掏網 - it200.com
相關函數:
- print():用于輸出一個或多個字符串。愛掏網 - it200.com
- fprint():用于將格式化的字符串寫入流中。愛掏網 - it200.com
sscanf(string,format,arg1,arg2,arg++) ;
參數 | 描述 | 必填/可選 |
---|---|---|
字符串 | 指定要讀取的字符串。愛掏網 - it200.com | 必填 |
格式 | 指定要使用的格式。愛掏網 - it200.com | 必填 |
- %% :返回百分號。愛掏網 - it200.com
- %c :ASCII 值。愛掏網 - it200.com
- %d :有符號的十進制數。愛掏網 - it200.com
- %e :科學計數法(例如 1.2e+2)。愛掏網 - it200.com
- %u :無符號的十進制數。愛掏網 - it200.com
- %f :浮點數。愛掏網 - it200.com
- %o :八進制數。愛掏網 - it200.com
- %s :字符串。愛掏網 - it200.com
- %x :十六進制數(小寫字母)。愛掏網 - it200.com
- %X :十六進制數(大寫字母)等。愛掏網 - it200.com
arg1
指定存儲數據的第一個變量。愛掏網 - it200.com
arg2
指定存儲數據的第二個變量。愛掏網 - it200.com
arg++
指定第三個、第四個等等。愛掏網 - it200.com
示例1
<?php
str = "PHP:7";
sscanf(str,"PHP:%d",language);
// show types and values
var_dump(language);
?>
輸出:
int(7)
示例2
<?php
str = "age:18 height:6ft";
sscanf(str,"age:%d height:%dft",age,height);
// show types and values
var_dump(age,height);
?>
輸出:
int(18)
int(6)
示例3
<?php
str = "Tutorial Website:javatpoint";
sscanf(str,"Tutorial Website:%s",site);
// show types and values
var_dump(site);
?>
輸出:
string(10) "javatpoint"
示例4
<?php
str = "We are Learning PHP 7";format = sscanf(str,"%s %s %s %s %c");
print_r(format);
?>
輸出:
Array
(
[0] => We
[1] => are
[2] => Learning
[3] => PHP
[4] => 7
)
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。