当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > sscanf() 函数

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 sscanf() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 437 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The sscanf() function parses input from a string according to a specified format. The sscanf() function parses a string into variables based on the format string.
sscanf()函数的作用是:按照一定格式解析输入的字符串。

If only two parameters are passed to this function, the data will be returned as an array. Otherwise, if optional parameters are passed, the data parsed are stored in them. If there are more specifiers than variables to contain them, an error occurs. However, if there are less specifiers than variables, the extra variables contain NULL.
如果向该函数传递了两个必要参数,那么数据将以数组的形式返回。否则,如果传递了可选参数,那么结息后的数据将被存入这些参数之中。如果指定的参数值多于变量的总数(这些指定的参数值是赋在变量上的),那么将产生错误;如果指定的参数值少于变量的总数,那么伟被赋值的变量将显示空值。

Syntax
语法

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
    %% -返回百分号
  • %b - Binary number
    %b –返回二进制数
  • %c - The character according to the ASCII value
    %c –返回与ASCII值相对应的字符
  • %d - Signed decimal number
    %d –带有正负号的十进制数
  • %e - Scientific notation (e.g. 1.2e+2)
    %e –科学计数符号(如:1.2e+2)
  • %u - Unsigned decimal number
    %u –不带正负号的十进制数
  • %f - Floating-point number (local settings aware)
    %f – 浮点数据(本地设置)
  • %F - Floating-point number (not local settings aware)
    %F –浮点数据(非本地设置)
  • %o - Octal number
    %o –十进制数
  • %s - String
    %s –字符串
  • %x - Hexadecimal number (lowercase letters)
    %x –十六进制数(小写字母)
  • %X - Hexadecimal number (uppercase letters)
    %X –十六进制数(大写字母)

Additional format values. These are placed between the % and the letter (example %.2f):
其它格式的值。它是位于%和字母之间的(如:%.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)
    ’(指定使用什么作为补白,默认值是空格。它必须与宽度指定器一起使用。如:%'x20s(使用“x”作为padding))
  • - (Left-justifies the variable value)
    - (左调整变量值)
  • [0-9] (Specifies the minimum width held of to the variable value)
    [0-9](指定变量值的最小宽度)
  • .[0-9] (Specifies the number of decimal digits or maximum string length)
    .[0-9](指定十进制数值或最大字符串长度)

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
必要参数。这个自变量(arg1)必须安插在第一个%-符号前
arg2 Optional. The second variable to store data in
可选参数。这个自变量(arg2)必须安插在第二个%-符号前
arg++ Optional. The third, fourth, and so on, to store data in
可选参数。与上述自变量相同,它们可以安插在第三个、第四个……(依次类推)%-符号前。


 

Example 1
案例1

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

The output of the code above will be:
上述代码将输出下面的结果:

int(30)
int(60)

评论 (0) All

登陆 | 还没注册?