Current Location: Home> Latest Articles> Notes on using sprintf with json_encode()

Notes on using sprintf with json_encode()

gitbox 2025-04-28

In PHP programming, sprintf and json_encode() are two very commonly used functions. sprintf is used to format strings, while json_encode() is used to convert PHP data structures into strings in JSON format. Although they each have powerful functions, if used accidentally, some problems or safety risks may be introduced. Today we will analyze the common pitfalls in the use of these two functions and discuss how to avoid them.

Common pitfalls of sprintf

The sprintf function can format the passed parameters into the specified string based on the given format string. Common usage scenarios include inserting data into SQL query statements, constructing log messages, etc. However, improper use of sprintf may lead to the following problems.

1. Format string error

The first parameter of printf and sprintf is the format string, and the subsequent parameters will be formatted according to the format. If the format string is wrong, it may result in incorrect output or more serious errors.

For example:

 $number = 123;
echo sprintf("%d is the number", $number);  // Correct output:123 is the number
echo sprintf("%s is the number", $number);  // Error output:123 is the number

In the above example, %s is used to format strings, not numbers, and %d is used to format integers. If you don't pay attention to the matching of formatters and data types, unexpected results may result.

2. SQL injection risk

sprintf is often used to build SQL queries. SQL injection vulnerabilities can be caused if user input is directly embedded into the SQL query string without proper escape or parameterized queries. For example:

 $username = $_GET['username'];
$query = sprintf("SELECT * FROM users WHERE username = '%s'", $username);

If $username is entered by the user and is not properly filtered or escaped, an attacker may enter malicious SQL statements to attack the database.

Security practice: Always use parameterized queries instead of embedding user input directly into SQL queries. Both PDO and MySQLi of PHP support parameterized queries, and they are recommended to avoid the risk of SQL injection.

3. Array and object formatting issues

sprintf cannot directly format arrays or objects. While %s can be used to format an array or object, it will only call the __toString() method, or output it directly as a string, which is usually not the result you want. For example:

 $array = [1, 2, 3];
echo sprintf("Array: %s", $array);  // Output:Array: Array

Safety practice: If you need to format an array or object, you can convert it to a string first, which can usually be implemented using json_encode() or implode() .

Common pitfalls of json_encode()

json_encode() is used to convert PHP data structures into strings in JSON format. This function is very common in API development and data exchange. However, in actual use, json_encode() may also cause some trouble.

1. Non-UTF-8 strings

json_encode() only supports correctly converting UTF-8-encoded strings to JSON. If you try to encode strings of other character sets (such as GBK), json_encode() returns false and does not give an explicit error message. To avoid this problem, make sure that the strings you pass in are UTF-8 encoded.

 $string = "This is a Chinese text";
echo json_encode($string);  // 正常Output: "This is a Chinese text"

If the string is not UTF-8 encoding, json_encode() will return false , and you may not be able to catch this error directly.

Safety practice: Use mb_convert_encoding() or make sure that the string fetched from the database is already UTF-8 encoded.

2. Error JSON format

json_encode() cannot handle some special data structures, such as resource ( resource ) and closure ( closure ). If you try to encode this data, json_encode() will return false .

 $resource = fopen('file.txt', 'r');
echo json_encode($resource);  // return:false

Safety practice: Make sure that the data does not contain resource types before passing to json_encode() , or process them with appropriate conversion functions.

3. json_encode() returns false

If json_encode() returns false , it means that an error occurred during the encoding process, but it will not provide specific error information. For better debugging, you can use json_last_error() to get the error code and json_last_error_msg() to get the error message.

 $data = ['key' => "\xB1\x31"];
$json = json_encode($data);
if ($json === false) {
    echo 'JSON mistake: ' . json_last_error_msg();
}

Summary: How to safely use sprintf and json_encode()

  1. Correctly use sprintf to format strings : make sure that the formatting matches the data type, avoid SQL injection vulnerabilities, and convert them to strings first when formatting an array.

  2. Avoid passing non-UTF-8 encoded strings to json_encode() : Make sure that the strings passed to json_encode() are valid UTF-8 encoded.

  3. Handle the situation where json_encode() returns false : use json_last_error_msg() to debug and find out the specific cause of encoding errors.

  4. Avoid formatting resources or objects : Process arrays, objects or resources in the appropriate way to avoid causing unpredictable errors.

By following these security specifications, the common pitfalls when using sprintf and json_encode() can be effectively avoided, thereby improving the security and robustness of the code.


End of the article