Home >Backend Development >PHP Problem >How to implement replacement in php sprintf

How to implement replacement in php sprintf

藏色散人
藏色散人Original
2021-12-20 09:58:392284browse

php sprintf method to implement replacement: 1. Create a PHP sample file; 2. Pass the specified value through "sprintf("There are %u million cars in %s.",$number,$str);" Just replace the symbols.

How to implement replacement in php sprintf

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

php sprintf How to implement replacement?

sprintf() function writes a formatted string into a variable.

arg1, arg2, parameters will be inserted into the main string at the percent sign (%) symbol. This function is executed step by step. At the first % sign, insert arg1, at the second % sign, arg2, and so on.

Note: If there are more % symbols than arg arguments, you must use placeholders. The placeholder follows the % symbol and consists of a number and "\$". See example 2.

Tips: Related functions: printf(), vprintf(), vsprintf(), fprintf() and vfprintf()

Example

The percent sign (%) symbol is replaced with a variable passed as a parameter:

<?php
$number = 2;
$str = "Shanghai";
$txt = sprintf("There are %u million cars in %s.",$number,$str);
echo $txt;
?>

Running result:

There are 2 million cars in Shanghai.

sprintf syntax:

sprintf(format,arg1,arg2,arg)

How to implement replacement in php sprintf

## Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to implement replacement in php sprintf. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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