sscanf Parses input from a string according to a format php函数


sscanf

(PHP 4 >= 4.0.1, PHP 5)

sscanfParses input from a string according to a format

说明

mixed sscanf ( string $str , string $format [, mixed &$... ] )

The function sscanf() is the input analog of printf(). sscanf() reads from the string str and interprets it according to the specified format, which is described in the documentation for sprintf().

Any whitespace in the format string matches any whitespace in the input string. This means that even a tab in the format string can match a single space character in the input string.

参数

str

The input string being parsed.

format

The interpreted format for str, which is described in the documentation for sprintf() with following differences:

  • Function is not locale-aware.
  • F, g, G and b are not supported.
  • D stands for decimal number.
  • i stands for integer with base detection.
  • n stands for number of characters processed so far.

...

Optionally pass in variables by reference that will contain the parsed values.

返回值

If only two parameters were passed to this function, the values parsed will be returned as an array. Otherwise, if optional parameters are passed, the function will return the number of assigned values. The optional parameters must be passed by reference.

范例

Example #1 sscanf() Example

<?php
// getting the serial number
list($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($month03) . "-$day ";
?>

If optional parameters are passed, the function will return the number of assigned values.

Example #2 sscanf() - using optional parameters

<?php
// get author info and generate DocBook entry
$auth "24 Lewis Carroll";
$n sscanf($auth"%d %s %s"$id$first$last);
echo 
"<author id='$id'>
    <firstname>
$first</firstname>
    <surname>
$last</surname>
</author> "
;
?>

参见



相关阅读:
stat 给出文件的信息
strcasecmp 二进制安全比较字符串(不区分大小写)
strchr 别名 strstr
strcmp 二进制安全字符串比较
strcoll 基于区域设置的字符串比较
strcspn 获取不匹配遮罩的起始子字符串的长度
stream_bucket_append Append bucket to brigade
stream_bucket_make_writeable Return a bucket object from the brigade for operating on
stream_bucket_new Create a new bucket for use on the current stream
stream_bucket_prepend Prepend bucket to brigade
stream_context_create Create a streams context
stream_context_get_default Retrieve the default streams context
stream_context_get_options Retrieve options for a stream/wrapper/context
stream_context_get_params Retrieves parameters from a context
快速导航

Copyright © 2016 phpStudy |