php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #71453 sscanf() ignores format text after string placeholder
Submitted: 2016-01-26 11:14 UTC Modified: 2021-01-13 14:55 UTC
From: dormilich at netscape dot net Assigned: cmb (profile)
Status: Closed Package: *General Issues
PHP Version: 5.6.17 OS: Mac OS X (10.11.3)
Private report: No CVE-ID: None
 [2016-01-26 11:14 UTC] dormilich at netscape dot net
Description:
------------
When the format string contains text after the last placeholder, which must be a string (i.e. %s), the variable is not only assigned the intended value, but also the text coming after it.

Test script:
---------------
<?php
sscanf('<report>cf2b27b5556c2ddc</report>', '<report>%s</report>', $hex);
echo $hex;
?>

Expected result:
----------------
cf2b27b5556c2ddc

Actual result:
--------------
cf2b27b5556c2ddc</report>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-28 15:02 UTC] dormilich at netscape dot net
this bug occurs for every string placeholder, not just the last one.
 [2016-01-31 15:23 UTC] bwoebi@php.net
-Type: Bug +Type: Documentation Problem
 [2016-01-31 15:23 UTC] bwoebi@php.net
Marking as doc problem as it actually doesn't document that %s is greedy and will only halt on whitespaces (or null-byte or string end) and doesn't backtrack (unlike regex).

But behavior is intentional.

For your usage case, use preg_match().
 [2021-01-13 14:55 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2021-01-13 14:55 UTC] cmb@php.net
This has been fixed[1] in the meantime.

[1] <https://github.com/php/doc-en/commit/34bec0531adc622a950f30029cf6d54d98e7f8d4>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 03:01:28 2024 UTC