I'm trying to sscanf
to read a bunch of successive fixed length strings but it's not working very well. I can print a fixed length string thusly:
sprintf('%.5s', 'aaaaabbbbb');
But if I try to use %.5s
to read a fixed length string (eg. the first 5 bytes of an input string) it doesn't work. eg.
var_dump(sscanf('aaaaabbbbb', '%.5s'));
If I do that var_dump
returns NULL
and I get the following Warning:
Warning: sscanf(): Bad scan conversion character "."
I tried %5s
in addition to %.5s
but that doesn't work as desired either. eg.
var_dump(sscanf('aa aabbbbb', '%5s'));
That returns this:
array(1) {
[0]=>
string(2) "aa"
}
What I'd want it to return is this:
array(1) {
[0]=>
string(5) "aa aa"
}
Any ideas?
The problem is that any space character is considered as a new input using %s
.
According to http://php.net/manual/en/function.sscanf.php
comment, this should work :
$result = sscanf(" Vendor: My Vendo Model: Super Model Foo Rev: 1234",
' Vendor: %8[ -~] Model: %16[ -~] Rev: %4c',
$vendor, $model, $rev);
So, in your case :
var_dump(sscanf('aa aabbbbb', '%5[ -~]'));
It works fine on PHP 5.2.10.