1

I have a trouble with PHP sscanf function and comma character. I need to extract 4 values, as this working example does:

<?php
$format = '%s %s, blabla %ih blabla %ih';
$input = 'blabla 08-06-2016 blabla 14h blabla 17h';
$result = sscanf($input, $format);
var_dump($result);

output:

array (size=4)
  0 => string 'blabla' (length=10)
  1 => string '08-06-2016' (length=10)
  2 => int 14
  3 => int 17

but my real example involves one comma character, and I do not get it to work:

<?php
$format = '%s %s, blabla %ih blabla %ih';
$input = 'blabla 08-06-2016, blabla 14h blabla 17h';
$result = sscanf($input, $format);
var_dump($result);

output:

array (size=4)
  0 => string 'blabla' (length=10)
  1 => string '08-06-2016,' (length=11)
  2 => null
  3 => null

It's very simple, but I can't find the solution... Is comma a reserved word?

UPDATE 1:

Sorry, I forgot to mention this: I wish to use the string template stored on $format as a template to sprintf too: If I modify it including regexp, it will not be sprintf compatible, am I wrong? I have supossed that same string template can serve to two directions: wrapping and unwrapping: very useful!... maybe is not possible with a comma involved?

Thanks :)

Katapofatico
  • 750
  • 1
  • 10
  • 29

1 Answers1

0

This will solve your issue read comma-separated input with scanf.

$format = '%s %20[^,], blabla %ih blabla %ih'; or some other max length, the 20 is arbitrary since I have no context in your application.

Community
  • 1
  • 1
Steve
  • 776
  • 6
  • 13