PHP 字符串函数

PHP sprintf() 函数用于格式化字符串并保存到一个变量。 

sprintf() 函数类似于 printf() 函数,但两者的唯一区别是 sprintf() 函数需要借助echo将内容输出到浏览器,而 printf() 函数可以直接在浏览器输出格式化为文本内容。

语法

语法如下:

sprintf(format, agr1, agr2, arg3...) : string

    这里的arg1、arg2、arg3等是sprintf()的参数。这些参数将插入带有百分比 (%) 符号的主字符串中。在每个 % 符号处,参数将一个接一个地插入替换。

    参数

    参数描述
    format

    必需。规定字符串以及如何格式化其中的变量。

    可能的格式值:

    • %% - 返回一个百分号 %
    • %b - 二进制数
    • %c - ASCII 值对应的字符
    • %d - 包含正负号的十进制数(负数、0、正数)
    • %e - 使用小写的科学计数法(例如 1.2e+2)
    • %E - 使用大写的科学计数法(例如 1.2E+2)
    • %u - 不包含正负号的十进制数(大于等于 0)
    • %f - 浮点数(本地设置)
    • %F - 浮点数(非本地设置)
    • %g - 较短的 %e 和 %f
    • %G - 较短的 %E 和 %f
    • %o - 八进制数
    • %s - 字符串
    • %x - 十六进制数(小写字母)
    • %X - 十六进制数(大写字母)

    附加的格式值。必需放置在 % 和字母之间(例如 %.2f):

    • + (在数字前面加上 + 或 - 来定义数字的正负性。默认情况下,只有负数才做标记,正数不做标记)
    • ' (规定使用什么作为填充,默认是空格。它必须与宽度指定器一起使用。例如:%'x20s(使用 "x" 作为填充))
    • - (左调整变量值)
    • [0-9] (规定变量值的最小宽度)
    • .[0-9] (规定小数位数或最大字符串长度)

    注释:如果使用多个上述的格式值,它们必须按照以上顺序使用。

    arg1必需。规定插到format字符串中第一个 % 符号处的参数。
    arg2可选。规定插到format字符串中第二个 % 符号处的参数。
    arg++可选。规定插到format字符串中第三、四等 % 符号处的参数。

    返回值

    返回格式化字符串。

    注意

    sprintf() 需要使用echo输出到浏览器。 sprintf()函数返回的格式化字符串在浏览器上通过echo打印出来,而printf()函数可以直接输出格式化的结果到浏览器。

    支持的版本

    PHP 4及以上版本支持此功能。

    示例

    下面给出一些示例学习 sprintf() 函数的使用方法。

    示例1

    简单示例,没有百分号。

    <?php
    $format 'this is a simple example';
    $res sprintf($format);
    echo $res;
    ?>
    • 1
    • 2
    • 3
    • 4

    输出:

    this is a simple example

    示例2

    变量声明

    <?php
    $quantity 1;
    $language 'sprintf';
    $format 'This is the  %dst example of the %s function.';
    $res sprintf($format$quantity$language);
    echo $res;
    echo '</br>';
    echo sprintf("this function works with echo.");
    ?>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    输出:

    This is the 1st example of the sprintf function.
    this function works with echo.

    示例3 

    参数顺序问题,%号的位置一般要与参数一一对应。

    <?php
    $num 54;
    $course 'PHP training';
    $year 2022;
    $format 'There are %d students in %s batch in the %d year.';
    echo $res sprintf($format$num$course$year);
    ?>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    输出:

    There are 54 students in PHP training batch in the 2022 year.

    在这里,如果我们在格式字符串中交换占位符的顺序,那输出结果就语义不通顺了。让我们看看下面的代码。

    <?php
    $num 54; $year = 2022;
    $course 'PHP training';
    $format 'There are %d students in %s batch in the %d year';
    echo $res sprintf($format$course$num$year);
    ?>
    • 1
    • 2
    • 3
    • 4
    • 5

    输出:

    There are 0 students in 54 batch in the 2022 year

    所以,如果我们想保留代码原样并想正确指出哪个占位符引用的参数,意思是占位符绑定参数顺序,然后修改代码如下:

    <?php
    $num 54;
    $course 'PHP training';
    $year 2022;
    $format 'There are %2$d students in %1$s batch in the %3$d year';
    echo $res sprintf($format$course$num$year);
    ?>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    输出:

    现在,输出与原始输出相同。

    There are 54 students in PHP training batch in the 2022 year

    注意:在这种情况下,我们需要定义参数的位置来打印代码的正确输出。

    示例4  

    指定填充字符

    <?php
    echo sprintf("%'.8d\n",1234);
    echo '</br>';
    echo sprintf("%'.08d\n",1234);
    ?>
    • 1
    • 2
    • 3
    • 4

    输出:

    现在,上面的填充字符代码的输出将是

    ....1234
    00001234

    示例5 

    指定填充字符

    <?php
    $snum 3259461827;
    echo sprintf("%.2e"$snum);		
    echo '</br>';
    
    echo sprintf("%'*6s\n""Hi");		
    echo '</br>';
    echo sprintf("%'*-6s\n""Hi");		
    echo '</br>';
    
    $fnum 125.235;
    echo sprintf("%f\n"$fnum);		
    echo '</br>';
    echo sprintf("%.2f\n"$fnum);		
    echo '</br>';
    echo sprintf("%.0f\n"$fnum);		
    echo '</br>';
    echo sprintf("%.8f\n"$fnum);		
    ?>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    输出:

    现在,上面代码的输出对于填充字符将输出:

    3.26e+9
    ****Hi
    Hi****
    125.235000
    125.23
    125
    125.23500000

    sprintf() 和 printf() 函数的区别

    sprintf() 和 printf() 函数之间的区别是 sprintf( ) 函数需要echo输出文本内容,而 printf() 函数不需要 echo 来显示文本。通过下面的例子了解它们的区别。

    示例

    <?php
    $str1 'We tried to printed on the browser directly using sprint() function.';
    sprintf($str1);
    $format 'This string is print on the browser with the help of echo function.';
    $str2 sprintf($format);
    echo $str2;
    ?>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    输出:

    在这里,我们可以看到变量$str1存储的文本并没有被sprintf()函数直接打印到浏览器上,所以我们使用echo来显示str2变量存储的字符串。

    This string is print on the browser with the help of echo function.

    现在,让我们看看printf的函数。

    <?php
    $str1 'This string is printed on the browser directly on the browser without using echo.';
    printf($str1);
    ?>
    • 1
    • 2
    • 3

    输出:

    This string is printed on the browser directly on the browser without using echo.