search
HomeBackend DevelopmentPHP TutorialNew uses of PHP's custom printf function

New uses of PHP's custom printf function

【相关学习推荐:php编程(视频)】

大家都知道 libc 的 printf() 及其家族。本章节将详细介绍 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' */

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' */

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

if (slprintf(foo, sizeof(foo), "%s", str) <p>记住:</p>
  • 这两个函数总是以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);</time.h>

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);</time.h>

注意

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

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);

关于 zend_ API 的注释

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

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


想了解更多编程学习,敬请关注php培训栏目!

The above is the detailed content of New uses of PHP's custom printf function. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools