I am trying to understand how the sscanf() function works in PHP
In both code snippits below, I do not get any formatted output. What I am doing wrong with the sscanf() function?
Thanks
Snipit #1
<?php
$input = '1235551234';
sscanf($input, "%3s%3s$4s", $a, $b, $c);
echo '(' . $a . ') ' . $b . '-' . $c;
?>
Returns: () -
Smipit #2
<?php
$input = 1235551234;
sscanf($input, "%3s%3s$4s", $a, $b, $c);
echo '(' . $a . ') ' . $b . '-' . $c;
?>
Returns: () -
sscanf($input, "%3s%3s$4s", $a, $b, $c);
^---- typo? should be %?
With that $
, it's probably looking for literal char sequence $
, 4
, s
which isn't present in your number.
Please try this. You had mistake in format.
$input = '1235551234';
sscanf($input, "%3s%3s%4s", $a, $b, $c); // good
// sscanf($input, "%3s%3s$4s", $a, $b, $c); // bad
echo '(' . $a . ') ' . $b . '-' . $c;