字串函式庫

sscanf

(PHP4 >= 4.0.1)

sscanf ---  依照格式剖析字串

語法 : mixed sscanf (string str, string format [, string var1...])

說明 : 

sscanf( )輸入的參數和printf( )類似,sscanf( )讀取字串 str,並且依照指定的格式 format來解釋它。如果只傳遞二個參數給此函式,則傳回剖析後的值將會是個陣列。

Example :

<?php

    // getting the serial number 

    $serial = sscanf("SN/2350001","SN/%d"); 

    // and the date of manufacturing 

    $mandate = "January 01 2000"; 

    list($month, $day, $year) = sscanf($mandate,"%s %d %d"); 

    echo "Item $serial was manufactured on: $year-".substr($month,0,3)."-$day\n";

?>

如果有傳遞非必需參數時,函式將會傳回分配值的數目,非必需參數必須依照關係傳遞。

Example :

<?php

    // get author info and generate DocBook entry 

    $auth = "24\tLewis Carroll"; 

    $n = sscanf($auth,"%d\t%s %s", &$id, &$first, &$last); 

    echo "<author id='$id'> <firstname>$first</firstname> <surname>$last</surname> </author>\n";

?>

參考 : fscanf( )  printf( )  sprintf( )


上一頁 首頁 下一頁