字符串函数库

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( )


上一页 首页 下一页