The code is as follows
代码如下 |
复制代码 |
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "
CreateTime>%s
0
";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
|
|
Copy code
|
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "
CreateTime>%s
0
";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
Many friends may not know why they write it like this after reading it. What does the %S behind it mean? I will get into the topic below
Grammar
string sprintf ( string $format [, mixed $args [, mixed $... ]] )
Parameter Description
format required. Convert format.
args optional. Specifies parameters to be inserted into the format string at % symbols.
Description
The argument format string is composed of zero or more directives: normal characters (except %) will be copied directly to the result, conversion indicators, each result takes its own argument. This applies to sprintf() and printf() .
The format parameter is the format of the conversion, starting with the percent sign ("%") and ending with the conversion character. Possible format values below:
1. An optional sign indicator forces a sign (- or +) to be used on a number. By default, only the – sign is used on a number if it is a negative value. This indicator also forces positive numbers to be appended with a + sign.
2. An optional padding indicator is what character will be used to pad the result to the correct string length. This may be a blank character or a zero character. Blanks are padded by default. A replacement can be specified Fill characters are prefixed by a single quote (').
3. An optional alignment indicator says whether the result should be left-aligned or right-aligned. The default is right-aligned; a - character here will make it left-aligned.
4. An optional number, a width indicator that says how many characters (minimum) this conversion should result in.
| 5. An optional precision indicator is in the form of a period ('.') followed by an optional string of decimal digits, that is, how many decimal digits should be displayed as floating point numbers . When this indicator is used within a string it acts as a break point, setting a maximum character limit for the string.