PHP sscanf() 函数
PHP 教程
· 2019-01-29 22:14:26
实例
Parse a string:
<?php
$str = "age:30 weight:60kg";
sscanf($str,"age:%d weight:%dkg",$age,$weight);
// show types and values
var_dump($age,$weight);
?>
$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++)
参数 | 描述 |
---|---|
string | 必需。规定要读取的字符串。 |
format | 必需。规定要使用的格式。 可能的格式值:
附加的格式值。必需放置在 % 和字母之间(例如 %.2f):
注释:如果使用多个上述的格式值,它们必须按照上面的顺序进行使用,不能打乱。 |
arg1 | 可选。存储数据的第一个变量。 |
arg2 | 可选。存储数据的第二个变量。 |
arg++ | 可选。存储数据的第三、四个变量。依此类推。 |
技术细节
返回值: | 如果只向该函数传递两个参数,数据将以数组的形式返回。否则,如果传递了额外的参数,那么被解析的数据会存储在这些参数中。如果区分符的数目大于包含它们的变量的数目,则会发生错误。不过,如果区分符的数目小于包含它们的变量的数目,则额外的变量包含 NULL。 |
---|---|
PHP 版本: | 4.0.1+ |
更多实例
实例 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);
?>
$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 教程 文章: https://www.codercto.com/courses/l/5.html
The Intersectional Internet
Safiya Umoja Noble、Brendesha M. Tynes / Peter Lang Publishing / 2016
From race, sex, class, and culture, the multidisciplinary field of Internet studies needs theoretical and methodological approaches that allow us to question the organization of social relations that ......一起来看看 《The Intersectional Internet》 这本书的介绍吧!