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.
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)
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!