Table of Contents
传统用途
你知道结果缓冲区的大小
你不知道缓冲区大小
那么 printf() 呢?
特殊的 PHP printf 格式
Printf() 到 zend_strings
关于 zend_ API 的注释
Home Backend Development PHP Tutorial How to customize printf function in PHP

How to customize printf function in PHP

Jul 28, 2020 pm 04:33 PM
php printf customize

How to customize printf function in PHP

大家都知道 libc 的 printf() 及其家族。本章节将详细介绍 PHP 声明和使用的许多克隆,它们的目标是什么,为什么使用它们,以及何时使用它们。

相关学习推荐:PHP编程从入门到精通

注意

Libc 中关于 printf() 及其朋友的文档位于此处。

你知道这些函数很有用,但有时无法提供足够的功能。另外,你知道向 printf()添加格式字符串并非易事,没有便携性和有安全风险。

PHP 添加了自己的类似于 printf 的函数,取代了 libc 的,并且由内部开发者使用。他们主要添加新的格式,并使用 zend_string代替 char *等等,让我们一起来看看。

警告

你必须掌握 libc 默认printf() 格式。请阅读它们的文档。

注意

添加了这些函数以 取代 libc 函数,意味着如果你使用了sprintf(),不会使用到 libc 的sprintf(),而是 PHP 取代了。除了传统的 printf()外,其他内容均被替换。

传统用途

首先,你不应该使用 sprintf(),因为该函数不执行任何检查,并且导致许多缓冲区溢出错误。请避免使用它。

警告

尽可能避免使用 sprintf()

然后,你有一些选择。

你知道结果缓冲区的大小

如果你知道缓冲区大小,snprintf() 或者 slprintf() 都可以使用。这些函数虽然在返回上不同,但是它们的功能是一样的。

这两个都是根据传递的格式来打印,并且无论发生什么,都会通过一个NUL 字节 ‘\0’来终止你的缓冲区。 但是,snprintf() 返回可以使用的字符数,而slprintf()返回可以有效使用的字符数,因此可以检测过小的缓冲区和字符串截断。这个不会计算最后的‘\0’

这里有个例子,以便你完全明白:

char foo[8]; /* 8字符大小的缓冲区 */
const char str[] = "Hello world"; /* 12个字符,包含 \0 */
int r;

r = snprintf(foo, sizeof(foo), "%s", str);
/* r = 11 ,即使这里只有7个可打印的字符可写入 foo */

/* foo 的值现在是 'H' 'e' 'l' 'l' 'o' ' ' 'w' '\0' */
Copy after login

snprintf() 不是一个好用的函数,因为它不允许检查最后的字符串截断。就像上面例子你看到的,显然“Hello world\0”不适合8字节的缓冲区,但是 snprintf() 仍然返回11给你,这是 strlen("Hello world\0") 的值。你没有办法检查字符串被截断了。

这是 slprintf()

char foo[8]; /* 8字符大的缓冲区 */
const char str[] = "Hello world"; /* 12个字符,包含 \0 */
int r;

r = slprintf(foo, sizeof(foo), "%s", str);
/* r = 7 ,因为7个可打印的字符被写入 foo */

/* foo 现在的值是 'H' 'e' 'l' 'l' 'o' ' ' 'w' '\0' */
Copy after login

使用 slprintf(),结果缓冲区 foo 包含完全相同的字符串,但是如今返回值为7。7少于 “Hello world” 字符串的11个字符,所以你可以检查它被截断了:

if (slprintf(foo, sizeof(foo), "%s", str) < strlen(str)) {
    /* 发生字符串截断 */
}
Copy after login

记住:

  • 这两个函数总是以NUL终止字符串,不管是否截断。最终的字符串是安全的 C 字符串。
  • 只有 slprintf()会检查字符串截断。

这两个函数在 main/snprintf.c 中有详细介绍。

你不知道缓冲区大小

现在如果你不知道结果缓冲区大小,则需要动态分配一个,并且使用spprintf()。记住,你必须自己释放缓冲区。

这是例子:

#include <time.h>

char *result;
int r;

time_t timestamp = time(NULL);

r = spprintf(&result, 0, "Here is the date: %s", asctime(localtime(&timestamp)));

/* 现在结果类似:"Here is the date: Thu Jun 15 19:12:51 2017\n" */

efree(result);
Copy after login

spprintf() 返回被打印到结果缓冲区的字符数,不包括最后的‘\0’, 因此,你知道分配给你的字节数(减一)是多少。

请注意,是使用 ZendMM(请求分配)分配的,因此应作为请求的一部分使用,并使用 efree() 而不是free()释放。

注意

Zend 内存管理章节 (ZendMM) 详细介绍如何通过 PHP 分配动态内存。

如果你想要限制缓冲区大小,则将限制传递给第二个参数,如果你传递 0,意味着无限制:

#include <time.h>

char *result;
int r;

time_t timestamp = time(NULL);

/* 打印不超过 10 个字节 ||分配超过 11 个字节 */
r = spprintf(&result, 10, "Here is the date: %s", asctime(localtime(&timestamp)));

/* r == 10,并且给结果分配 11 个字节 */

efree(result);
Copy after login

注意

尽可能不要使用动态内存分配。这会影响执性能。如果有选择,则选静态堆栈分配缓冲区。

spprintf()写在 main/spprintf.c 中。

那么 printf() 呢?

如果你需要 printf(),即打印格式化到输出流,则使用php_printf()。该函数在内部使用 spprintf(),因此执行动态分配,以便将其发送到 SAPI 输出(在 CLI 的情况下又称为 stdout),或输出缓冲区(CGI 缓冲区)后将其释放,用于其他 SAPI。

特殊的 PHP printf 格式

记住,PHP 通过自己设计,取代了很多 libc 的 printf() 函数。你可以从阅读源代码中查看易于理解的参数解析 API。

这意味着解析算法的参数已完全被重写,并且可能与你在 libc 使用的不同。即,在大多数情况下,不会关注 libc 环境。

可能会使用特殊的格式,就像 “%I64” 打印64位 int,或者“%I32”。你也可以使用 “%Z” 去打印 zval(根据 PHP 规则转换为字符串),这是一个不错的补充。

该格式化程序也认识无穷数,并打印 “INF”,或者将非数字打印为 “NAN”。

如果你错误的请求格式化程序打印一个 NULL 指针,libc 肯定会崩溃,而 PHP 会将 “(null)” 作为结果字符串返回。

注意

如果在打印中你看到神奇的 “(null)” 出现,意味着你将 NULL 指针传递给了 PHP printf 系列函数之一。

Printf() 到 zend_strings

zend_string 作为 PHP 源代码里非常常见的结构,你可能需要 printf()zend_string,而不是传统的 char *。为此,请使用strpprintf()

该 API 是 zend_string *strpprintf(size_t max_len, const char *format, ...) ,意味着返回zend_string 给你,而不是你期望的可打印字符数。不过你可以限制使用第一个参数来限制该数(传递 0 表示无穷大);并且你一定要记住将使用 Zend 内存管理分配 zend_string,并因此绑定当前请求。

显然,该格式 API 与上面看到的共享。

这有个例子:

zend_string *result;

result = strpprintf(0, "You are using PHP %s", PHP_VERSION);

/* 对结果做些什么 */

zend_string_release(result);
Copy after login

关于 zend_ API 的注释

您可能会遇到 zend_spprintf()zend_strpprintf() 函数。这些与上面看到的完全相同。

这只是 Zend 引擎和 PHP 核心之间分离的一部分,这个细节对我们并不重要,因为在源码中,所有内容都是混合在一起的。


The above is the detailed content of How to customize printf function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles