sscanf() parses string into variables based on the format string. The format string uses the same conversion specifiers as sprintf() ; however, instead of formatting and transforming variables, it provides a template with which to parse a string into variables. See sprintf() for a complete list of conversion specifiers.
This function accepts two or more arguments. If only two arguments are provided, the data parsed from string are returned as a numerically keyed array. If additional arguments are passed to the function, the data parsed out of string are stored in them. If there are more specifiers than variables to contain them, an error is generated. If the opposite is true, the extra variables contain NULL .
sscanf() is best suited to dealing with simple strings that follow consistent fixed formats. For more robust functionality, use the Perl-style regular expression library.
<?php $string = 'age:27 height:1.83m weight:90kg'; sscanf($string, 'age:%d height:%fm weight:%dkg', $age, $height, $weight); // use var_dump to show the types, as well as the values var_dump($age, $height, $weight); ?> Output: int(27) float(1.83) int(90)
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.