PHP sprintf function instance based on JS, jsphpsprintf Function
This example describes how to implement the sprintf function of PHP Based on JS. We will share this with you for your reference. The details are as follows:
The function is as follows:
<Script type = "text/javascript"> function sprintf () {var arg = arguments, str = arg [0] | '', I, n; for (I = 1, n = arg. length; I <n; I ++) {str = str. replace (/% s/, arg [I]);} return str ;}</script>
The first parameter is the string containing "% s". Other parameters are the corresponding variables used to replace "% s.
For example:
<Script type = "text/javascript"> var str = "% s bright light in front of the bed, suspected to be % s frost; Raise your head % s bright moon, bow your head % s hometown. ", Var1 =" Ming ", var2 =" ", var3 =" Wang ", var4 =" Si "; str = sprintf (str, var1, var2, var3, var4); </script>
I hope this article will help you design JavaScript programs.