php对字符串格式化,PHP字符串格式化

本文详细介绍了sprintf函数的用法,包括如何在字符串中插入数值、格式化整数和浮点数,以及如何处理不同进制的数字。文章通过多个示例展示了如何动态指定宽度和精度,以及在遇到不同长度的字符串时如何正确连接。此外,还提到了sprintf在处理字符串连接时的注意事项,例如防止因未正确指定长度而导致的错误。
摘要由CSDN通过智能技术生成

Q:

格式是 CI00000000  当ID是 1时显示为 CI00000001

ID是 100 时显示为 CI00000100

谁有好的点子,

A:

$num=19;

$s='CI'.sprintf('%08d',$num);

解释:

%d –带有正负号的十进制数

Definition and Usage

定义和用法

The sprintf() function writes a formatted string to a variable.

sprintf()函数的作用是:输出格式化字符串到变量。

The arg1, arg2, ++ parameters will be inserted at percent (%) signs

in the main string. This function works “step-by-step”. At the first %

sign, arg1 is inserted, at the second % sign, arg2 is inserted, etc.

arg1, arg2, ++参数将被插入到主体字符串中的百分号(%)之后。这个函数是“一步一步[step-by-step]”执行的。在第一个“%”之后插入arg1,在第二个“%”之后插入arg2,依次类推。

Syntax语法

sprintf(format,arg1,arg2,arg++)

Parameter参数

Description描述

format

Required. Specifies the string and how to format the variables in it.

必要参数。指定字符串,以及如何定义其中变量的格式。

Possible format values:

可能值如下:

%% - Returns a percent sign

%% -返回百分号

%b - Binary number

%b –返回二进制数

%c - The character according to the ASCII value

%c –返回与ASCII值相对应的字符

%d - Signed decimal number

%d –带有正负号的十进制数

%e - Scientific notation (e.g. 1.2e+2)

%e –科学计数符号(如:1.2e+2)

%u - Unsigned decimal number

%u –不带正负号的十进制数

%f - Floating-point number (local settings aware)

%f – 浮点数据(本地设置)

%F - Floating-point number (not local settings aware)

%F –浮点数据(非本地设置)

%o - Octal number

%o –十进制数

%s - String

%s –字符串

%x - Hexadecimal number (lowercase letters)

%x –十六进制数(小写字母)

%X - Hexadecimal number (uppercase letters)

%X –十六进制数(大写字母)

Additional format values. These are placed between the % and the letter (example %.2f):

其它格式的值。它是位于%和字母之间的(如:%.2f)

+ (Forces both + and - in front of numbers. By default, only negative numbers are marked)

+(在数字前加上+和-;默认情况下,只有负数是被标记出来的)

(Specifies what to use as padding. Default is space. Must be used

together with the width specifier. Example: %’x20s (this uses “x” as

padding)

’(指定使用什么作为补白,默认值是空格。它必须与宽度指定器一起使用。如:%’x20s(使用“x”作为padding))

- (Left-justifies the variable value)

- (左调整变量值)

[0-9] (Specifies the minimum width held of to the variable value)

[0-9](指定变量值的最小宽度)

.[0-9] (Specifies the number of decimal digits or maximum string length)

.[0-9](指定十进制数值或最大字符串长度)

Note: If multiple additional format values are used, they must be in the same order as above.

注意:如果使用附加格式值,那么它必须与上述顺序相同

arg1

Required. The argument to be inserted at the first %-sign in the format string

必要参数。这个自变量(arg1)必须安插在第一个%-符号前

arg2

Optional. The argument to be inserted at the second %-sign in the format string

可选参数。这个自变量(arg2)必须安插在第二个%-符号前

arg++

Optional. The argument to be inserted at the third, fourth, etc. %-sign in the format string

可选参数。与上述自变量相同,它们可以安插在第三个、第四个……(依次类推)%-符号前。

Tips and Notes提示和注意点

Note: If there are more % signs than arguments, you

must use placeholders. A placeholder is inserted after the % sign, and

consists of the argument- number and “/$”. See example three.

注意:注意:如果这里的%比自变量更多,你必须使用占位符[placeholders]。占位符是安插在%之后的,它是由自变量-数字和“/$”组成的。具体可以见案例3。

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

提示:相关函数:printf(), sprintf(), vfprintf(), vprintf(), 和 vsprintf()

Example 1案例1

$txt = sprintf("%s world. Day number %u",$str,$number);e

cho $txt;?>

The output of the code above will be:

上述代码将输出下面的结果:

Hello world. Day number 123

Example 2

案例2

The output of the code above will be:

上述代码将输出下面的结果:

123.000000

Example 3

案例3

Use of placeholders:

使用占位符:

$txt = sprintf("With 2 decimals: %1/$.2f
With no decimals: %1/$u",$number);

echo $txt;?>

The output of the code above will be:

上述代码将输出下面的结果:

With 2 decimals: 123.00 With no decimals: 123

sprintf 最常见的应用之一莫过于把整数打印到字符串中,所以,spritnf

在大多数场合可以替代itoa。

这样,一个整数的16 进制字符串就很容易得到,但我们在打印16 进制内容

时,通常想要一种左边补0 的等宽格式,那该怎么做呢?很简单,在表示宽

度的数字前面加个0 就可以了。

sprintf(s, "%08X", 4567); //产生:"000011D7"

上面以”%d”进行的10 进制打印同样也可以使用这种左边补0 的方式。

这里要注意一个符号扩展的问题:比如,假如我们想打印短整数(short)-1

的内存16 进制表示形式,在Win32 平台上,一个short 型占2 个字节,所

以我们自然希望用4 个16 进制数字来打印它:

short si = -1;

sprintf(s, "%04X", si);

产生“FFFFFFFF”,怎么回事?因为spritnf 是个变参函数,除了前面两个

参数之外,后面的参数都不是类型安全的,函数更没有办法仅仅通过一个

“%X”就能得知当初函数调用前参数压栈时被压进来的到底是个4 字节的整

数还是个2 字节的短整数,所以采取了统一4 字节的处理方式,导致参数压

栈时做了符号扩展,扩展成了32 位的整数-1,打印时4 个位置不够了,就

把32 位整数-1 的8 位16 进制都打印出来了。如果你想看si 的本来面目,

那么就应该让编译器做0 扩展而不是符号扩展(扩展时二进制左边补0 而不

是补符号位):

sprintf(s, "%04X", (unsigned short)si);

就可以了。或者:

unsigned short si = -1;

sprintf(s, "%04X", si);

2:

浮点数的打印和格式控制是sprintf 的又一大常用功能,浮点数使用格式符”

%f”控制,默认保留小数点后6 位数字,比如:

sprintf(s, "%f", 3.1415926); //产生"3.141593"

但有时我们希望自己控制打印的宽度和小数位数,这时就应该使用:”

%m.nf”格式,其中m 表示打印的宽度,n 表示小数点后的位数。比如:

sprintf(s, "%10.3f", 3.1415626); //产生:" 3.142"

sprintf(s, "%-10.3f", 3.1415626); //产生:"3.142 "

sprintf(s, "%.3f", 3.1415626); //不指定总宽度,产生:"3.142"

注意一个问题,你猜

int i = 100;

sprintf(s, "%.2f", i);

会打出什么东东来?“100.00”?对吗?自己试试就知道了,同时也试试下面这个:

sprintf(s, "%.2f", (double)i);

第一个打出来的肯定不是正确结果,原因跟前面提到的一样,参数压栈时调

用者并不知道跟i相对应的格式控制符是个”%f”。而函数执行时函数本身

则并不知道当年被压入栈里的是个整数,于是可怜的保存整数i 的那4 个字

节就被不由分说地强行作为浮点数格式来解释了,整个乱套了。

3:

连接字符串

sprintf 的格式控制串中既然可以插入各种东西,并最终把它们“连成一

串”,自然也就能够连接字符串,从而在许多场合可以替代strcat,但

sprintf 能够一次连接多个字符串(自然也可以同时在它们中间插入别的内

容,总之非常灵活)。比如:

char* who = "I";

char* whom = "CSDN";

sprintf(s, "%s love %s.", who, whom); //产生:"I love CSDN. "

strcat 只能连接字符串(一段以’\0’结尾的字符数组或叫做字符缓冲,

null-terminated-string),

但有时我们有两段字符缓冲区,他们并不是以’\0’结尾。比如许多从第三

方库函数中返回的字符数组,从硬件或者网络传输中读进来的字符流,它们

未必每一段字符序列后面都有个相应的’\0’来结尾。如果直接连接,不管

是sprintf 还是strcat 肯定会导致非法内存操作,strncat 也至少要求第

一个参数是个null-terminated-string,那该怎么办呢?我们自然会想起前

面介绍打印整数和浮点数时可以指定宽度,字符串也一样的。比如:

char a1[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};

char a2[] = {'H', 'I', 'J', 'K', 'L', 'M', 'N'};

如果:

sprintf(s, "%s%s", a1, a2); //Don't do that!

十有八九要出问题了。是否可以改成:

sprintf(s, "%7s%7s", a1, a2);

也没好到哪儿去,正确的应该是:

sprintf(s, "%.7s%.7s", a1, a2);//产生:"ABCDEFGHIJKLMN"

这可以类比打印浮点数的”%m.nf”,在”%m.ns”中,m 表示占用宽度(字

符串长度不足时补空格,超出了则按照实际宽度打印),n 才表示从相应的

字符串中最多取用的字符数。通常在打印字符串时m 没什么大用,还是点号

后面的n 用的多。自然,也可以前后都只取部分字符:

sprintf(s, "%.6s%.5s", a1, a2);//产生:"ABCDEFHIJKL"

(因为sprintf函数将输出写入到字符串s中,并以'\0'结束,所以生成的s

中有'\0',所以可以用printf(s),而不用担心会出错)

在许多时候,我们或许还希望这些格式控制符中用以指定长度信息的数字是

动态的,而不是静态指定的,因为许多时候,程序要到运行时才会清楚到底

需要取字符数组中的几个字符,这种动态的宽度/精度设置功能在sprintf

的实现中也被考虑到了,sprintf 采用”*”来占用一个本来需要一个指定

宽度或精度的常数数字的位置,同样,而实际的宽度或精度就可以和其它被

打印的变量一样被提供出来,于是,上面的例子可以变成:

sprintf(s, "%.*s%.*s", 7, a1, 7, a2);

或者:

sprintf(s, "%.*s%.*s", sizeof(a1), a1, sizeof(a2), a2);

实际上,前面介绍的打印字符、整数、浮点数等都可以动态指定那些常量值,

比如:

sprintf(s, "%-*d", 4, 'A'); //产生"65 "

sprintf(s, "%#0*X", 8, 128); //产生"0X000080","#"产生0X

sprintf(s, "%*.*f", 10, 2, 3.1415926); //产生" 3.14"

posted on 2009-07-23 09:08 haix 阅读(3681) 评论(0)  编辑 收藏 引用 网摘 所属分类: PHP

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值