Приглашаем посетить
Спорт (www.sport-data.ru)

Scanning Formatted Strings

Previous
Table of Contents
Next

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.";
?>


Previous
Table of Contents
Next