位置: 编程技术 - 正文

PHP字符串函数sprintf()的用法(php字符串赋值)

编辑:rootadmin
sprintf

推荐整理分享PHP字符串函数sprintf()的用法(php字符串赋值),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:php字符串操作函数,php字符串型数据的定义方式,php字符串赋值,php字符串型数据的定义方式,php字符串定义的三种方式,php字符串型数据的定义方式,php字符串型数据的定义方式,php字符串型数据的定义方式,内容如对您有帮助,希望把文章链接给更多的朋友!

(PHP 4, PHP 5)

sprintf — Return a formatted string

说明 string sprintf ( string $format [, mixed $args [, mixed $... ]] )

Returns a string produced according to the formatting string format.

参数

format

The format string is composed of zero or more directives: ordinary characters (excluding %) that are copied directly to the result, and conversion specifications, each of which results in fetching its own parameter. This applies to both sprintf() and printf().

Each conversion specification consists of a percent sign (%), followed by one or more of these elements, in order: An optional sign specifier that forces a sign (- or +) to be used on a number. By default, only the - sign is used on a number if it&#;s negative. This specifier forces positive numbers to have the + sign attached as well, and was added in PHP 4.3.0. An optional padding specifier that says what character will be used for padding the results to the right string size. This may be a space character or a 0 (zero character). The default is to pad with spaces. An alternate padding character can be specified by prefixing it with a single quote (&#;). See the examples below. An optional alignment specifier that says if the result should be left-justified or right-justified. The default is right-justified; a - character here will make it left-justified. An optional number, a width specifier that says how many characters (minimum) this conversion should result in. An optional precision specifier in the form of a period (`.&#;) followed by an optional decimal digit string that says how many decimal digits should be displayed for floating-point numbers. When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit to the string.

A type specifier that says what type the argument data should be treated as. Possible types: % - a literal percent character. No argument is required. b - the argument is treated as an integer, and presented as a binary number. c - the argument is treated as an integer, and presented as the character with that ASCII value. d - the argument is treated as an integer, and presented as a (signed) decimal number. e - the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of significant digits (one less). E - like %e but uses uppercase letter (e.g. 1.2E+2). f - the argument is treated as a float, and presented as a floating-point number (locale aware). F - the argument is treated as a float, and presented as a floating-point number (non-locale aware). Available since PHP 4.3. and PHP 5.0.3. g - shorter of %e and %f. G - shorter of %E and %f. o - the argument is treated as an integer, and presented as an octal number. s - the argument is treated as and presented as a string. u - the argument is treated as an integer, and presented as an unsigned decimal number. x - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). X - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).

Variables will be co-erced to a suitable type for the specifier: Type Handling Type Specifiers string s integer d, u, c, o, x, X, b double g, G, e, E, f, F

Warning

Attempting to use a combination of the string and width specifiers with character sets that require more than one byte per character may result in unexpected results

The format string supports argument numbering/swapping. Here is an example:

Example #1 Argument swapping

<?php$num=5;$location='tree';$format='Thereare%dmonkeysinthe%s';echosprintf($format,$num,$location);?> This will output "There are 5 monkeys in the tree". But imagine we are creating a format string in a separate file, commonly because we would like to internationalize it and we rewrite it as:

Example #2 Argument swapping

<?php$format='The%scontains%dmonkeys';echosprintf($format,$num,$location);?> We now have a problem. The order of the placeholders in the format string does not match the order of the arguments in the code. We would like to leave the code as is and simply indicate in the format string which arguments the placeholders refer to. We would write the format string like this instead:

Example #3 Argument swapping

<?php$format='The%2$scontains%1$dmonkeys';echosprintf($format,$num,$location);?> An added benefit here is that you can repeat the placeholders without adding more arguments in the code. For example:

Example #4 Argument swapping

<?php$format='The%2$scontains%1$dmonkeys.That'sanice%2$sfullof%1$dmonkeys.';echosprintf($format,$num,$location);?> When using argument swapping, the n$ position specifier must come immediately after the percent sign (%), before any other specifiers, as shown in the example below. PHP字符串函数sprintf()的用法(php字符串赋值)

Example #5 Position specifier with other specifiers

<?php$format='The%2$scontains%1$dmonkeys';echosprintf($format,$num,$location);?>

以上例程会输出:

Note:

Attempting to use a position specifier greater than PHP_INT_MAX will result in sprintf() generating warnings.

Warning

The c type specifier ignores padding and width

args

...

返回值

Returns a string produced according to the formatting string format.

范例

Example #6 printf(): various examples

<?php$n=;$u=-;$c=;//ASCIIis'A'//noticethedouble%%,thisprintsaliteral'%'characterprintf("%%b='%b'n",$n);//binaryrepresentationprintf("%%c='%c'n",$c);//printtheasciicharacter,sameaschr()functionprintf("%%d='%d'n",$n);//standardintegerrepresentationprintf("%%e='%e'n",$n);//scientificnotationprintf("%%u='%u'n",$n);//unsignedintegerrepresentationofapositiveintegerprintf("%%u='%u'n",$u);//unsignedintegerrepresentationofanegativeintegerprintf("%%f='%f'n",$n);//floatingpointrepresentationprintf("%%o='%o'n",$n);//octalrepresentationprintf("%%s='%s'n",$n);//stringrepresentationprintf("%%x='%x'n",$n);//hexadecimalrepresentation(lower-case)printf("%%X='%X'n",$n);//hexadecimalrepresentation(upper-case)printf("%%+d='%+d'n",$n);//signspecifieronapositiveintegerprintf("%%+d='%+d'n",$u);//signspecifieronanegativeinteger?>

以上例程会输出:

Example #7 printf(): string specifiers

<?php$s='monkey';$t='manymonkeys';printf("[%s]n",$s);//standardstringoutputprintf("[%s]n",$s);//right-justificationwithspacesprintf("[%-s]n",$s);//left-justificationwithspacesprintf("[%s]n",$s);//zero-paddingworksonstringstooprintf("[%'#s]n",$s);//usethecustompaddingcharacter'#'printf("[%.s]n",$t);//left-justificationbutwithacutoffofcharacters?>

以上例程会输出:

Example #8 sprintf(): zero-padded integers

<?php$isodate=sprintf("%d-%d-%d",$year,$month,$day);?>

Example #9 sprintf(): formatting currency

<?php$money1=.;$money2=.;$money=$money1+$money2;//echo$moneywilloutput".1";$formatted=sprintf("%.2f",$money);//echo$formattedwilloutput"."?>

Example # sprintf(): scientific notation

<?php$number=;echosprintf("%.3e",$number);//outputs3.e+8?> 参见

printf() - 输出格式化字符串 sscanf() - 根据指定格式解析输入的字符 fscanf() - 从文件中格式化输入 vsprintf() - 返回格式化字符串 number_format() - 以千位分隔符方式格式化一个数字

PHP字符串函数sha1()的用法 sha1(PHP4=4.3.0,PHP5)sha1计算字符串的sha1散列值说明stringsha1(string$str[,bool$raw_output=false])利用美国安全散列算法1计算字符串的sha1散列值。参数str输入字符串。

PHP字符串函数sscanf()的用法 sscanf(PHP4=4.0.1,PHP5)sscanf根据指定格式解析输入的字符说明mixedsscanf(string$str,string$format[,mixed&$...])这个函数sscanf()输入类似printf()。sscanf()读取字符串str然后

PHP字符串函数soundex()的用法 soundex(PHP4,PHP5)soundexCalculatethesoundexkeyofastring说明stringsoundex(string$str)Calculatesthesoundexkeyofstr.Soundexkeyshavethepropertythatwordspronouncedsimilarlyproducethesamesoundexkey,andca

标签: php字符串赋值

本文链接地址:https://www.jiuchutong.com/biancheng/278836.html 转载请保留说明!

上一篇:PHP字符串函数str_ireplace()的用法(php str函数)

下一篇:PHP字符串函数sha1()的用法(php字符串定义的三种方式)

  • 借款合同印花税最新政策2023年
  • 个税专项附加继续教育
  • 注册公司工贸和商贸区别
  • 教育行业主营业务内容
  • 提完折旧的车卖掉划算吗
  • 支付宝理财提现到银行卡有费用吗
  • 不动产服务具体有哪些
  • 会议费报销税务规定
  • 年底向员工客户发放、赠送实物的怎么缴个税
  • 托收承付和委托收款的含义和相同之处
  • 票据贴现利息怎么做账
  • 开出增值税发票没收到怎么抵扣进项税?
  • 运输增值税专票含税价怎么算
  • 增值税专用发票开票必填项
  • 税收六项减免
  • 企业合同约定调岗不接受辞退没赔偿成立吗
  • 第二个季度
  • 公司买回来做样衣的服装怎么做会计分录?
  • 企业购入交易性金融资产支付的交易费用
  • 水利建设专项收入怎么申报不了
  • 转出未交增值税借方余额表示什么
  • 企业奠基费用如何入账
  • 荣耀x10升级鸿蒙系统好用吗
  • php的注释可以使用什么开头
  • 前端向后端传值的函数
  • 为什么要把收入当成舞弊假定
  • 一次性取得的租金收入
  • 发票抵扣联能报销吗
  • php 命名空间 通俗易懂
  • cifar10图像分类实验报告
  • 2021年车辆检测
  • php文件上传操作流程图
  • 如何使用扫描王
  • 农业合作社需要纳税吗
  • 上个月的留底税这个使用,会计分录
  • 公账转给员工工资
  • 免费赠送的产品报关金额
  • 小规模纳税人需要缴纳个人所得税吗
  • php composer 常用库
  • 进项税额不得从销项税额中抵扣是什么意思
  • 小规模增值税申报未开票收入怎么填
  • 投资性房地产转为存货
  • 合同结算属于资产吗
  • 应交税费的进项和销项是什么意思
  • 印花税计入相关资产成本吗
  • 应付职工薪酬账户贷方登记的是
  • 年末结余资金
  • 党委经费是国家政府出吗?
  • 出现事故保险公司负责协商吗
  • 描述企业会计准则中对固定资产的具体规定
  • 财务费用怎么记账
  • 一个完整的活动策划方案范文
  • 计算机上没有运行windows无线服务
  • 打开与关闭光驱怎么设置
  • ubuntu16.04创建用户
  • WIN10系统更新之后无法启动
  • linux系统中安装软件的批处理文件
  • window怎么开启自动更新
  • windows7的使用方法
  • win10在更新界面怎么办
  • win8.1使用
  • win7定时开关怎么定时
  • Android游戏开发入门
  • layui框架中修改用户成功后怎么跳转到登录界面
  • android:Fragment动画那点事
  • opengl3d
  • 关于jquery的描述错误的是
  • 安卓手机如何打开.icon文件
  • html、css和jquery相结合实现简单的进度条效果实例代码
  • js禁用键盘事件
  • Jquery $when done then的用法详解
  • 云阅卷查询成绩登录入口
  • 怎么查其他公司的财务报表
  • 建筑施工企业研发中心建设
  • 江苏无锡2023年GDP
  • 国税地税征管体制改革方案全文
  • 湖南省税务局网站2024公务员招聘
  • ca用户绑定怎么绑
  • 贷款抵押担保合同
  • 环保税2018年开征文件
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设