Scanning Formatted Strings


 sscanf($date, '%d/%d/%d') 


Another function related to printf() is sscanf(). This one parses a string and tries to match it with a pattern that contains placeholders. The $input string contains a date and is scanned using the string '%d-%d-%d' with several placeholders, as shown in the previous phrase. The function returns an array with all values for the matched placeholders. Then this array is passed to vprintf() to print it.

Scanning Formatted Strings (sscanf.php)
 <?php   $date = '02/01/06';   $values = sscanf($date, '%d/%d/%d');   vprintf('Month: %d; Day: %d; Year: %d.', $values); ?> 

Alternatively, you can provide a list of variable names as additional parameters to sscanf(). Then the function writes the substrings that match the placeholders into these variables. The following code shows this:

Scanning Formatted Strings (sscanf-alternative.php)
 <?php   $date = '02/01/06';   $values = sscanf($date, '%d/%d/%d', $m, $d, $y);   echo "Month: $m; Day: $d; Year: $y."; ?> 




PHP Phrasebook
PHP Phrasebook
ISBN: 0672328178
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net