PHP字符串格式化输出函数详解
1. 格式化输出功能概述
chop函数实际上是rtrim()函数的别名,用于去除字符串右侧的空格或指定字符。
ltrim()与trim()函数分别用于去除字符串左侧和两侧的空格或特定字符。
nl2br()函数能够将换行符\n转换为HTML中的<br>标签,以便在网页中正确显示换行效果。
print、echo、printf()和sprintf()是PHP中用于输出或格式化字符串的核心函数。
echo并非真正的函数,而是语言结构,无返回值;print是函数,具有返回值,返回布尔值false或true。
printf()函数用于将字符串按照指定格式进行输出。
printf()是一个非缓冲函数,直接调用系统IO接口进行文字格式化输出。例如:$name="hunte"; $age=25; printf("my name is %s, age %d", $name, $age);
sprintf()函数用于格式化字符串并将其赋值给变量,而不直接输出结果,其用法类似于C语言中的sprintf。
<?php
echo nl2br("foo isn't\n bar");
echo "foo isn't\n bar";
?>
与printf相似,但sprintf不打印内容,而是返回格式化后的字符串。例如:char sql[256]; sprintf(sql,"select * from table where no = '%s'",bankno); 该操作仅将格式化后的语句赋给变量sql。
strtolower、strtoupper、ucwords和ucfirst函数分别用于将字符串转为小写、大写、首字母大写(每个单词)和首字母大写。
2. 字符串的连接和分割操作
(1) array explode(string input, string separator, int limit):根据分隔符将字符串拆分为数组。
该函数使用一个字符串作为分隔符来分割另一个字符串。
<?php
// 示例 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
// 示例 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
?>
例子2. limit 参数示例
<?php
$str = 'one|two|three|four';
// 正数的 limit
print_r(explode('|', $str, 2));
// 负数的 limit
print_r(explode('|', $str, -1));
?>
string strtok(string input, string separator):基于分隔符进行标记化分割。
<?php
$string = "This is\tan example\nstring";
/* Use tab and newline as tokenizing characters as well */
$tok = strtok($string, " \n\t");
//拿着空格,\n,\t作为令牌分割字符串
while ($tok !== false) {
echo "Word=$tok
";
$tok = strtok(" \n\t");
}
?>
执行结果如下所示:
Word=This Word=is Word=an Word=example Word=string
(2) 字符串的截取操作
$test="Your customer service is excellent";
echo substr($test,1);////////our customer service is excellent
echo "
";
echo substr($test,-9);//////从末尾起长度是9excellent
echo "
";
echo substr($test,0,4);////从0位置开始长度是4Your
echo "
";
echo substr($test,5,-13);/从第四个开始到倒数第13个字符customer service
echo "
";
$test="Your customer service is excellent";
echo substr($test,1);
echo "
";
echo substr($test,-11);
echo "
";
echo substr($test,0,6);
echo "
";
echo substr($test,5,-13);
echo "
";
原始字符串为'our customer service is excellent',通过不同参数截取可得's excellent'、'Your c'或完整字符串等片段。
(3) join()函数用于将数组元素连接成一个字符串,实现字符串链接功能。
3. 字符串的查找功能
(1) string strstr(string haystack, string needle),别名strchr。stristr与strstr类似,但不区分大小写。
strrchr()函数相反,用于查找最后一次出现的子字符串。
此处指从第一次出现的位置开始提取的子字符串。
<?php
$email = 'user@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
?>
$email = 'user@example.com';
$domain =strstr($email,'e');
$domain2 =strrchr($email,'e');//最后一次出现起的字符串
echo $domain;
echo "
";
echo $domain2;
示例输出:er@example.com 和 e.com
(2) 查找子字符串在父字符串中的位置
int strpos(string str, string needle, [int offset]):若未找到则返回false。
该函数返回从offset位置开始在str中查找needle的起始位置。
$eg: $t='hello world';
echo strpos($t,'o',5);
//输出7,表示从第5个字符之后开始查找,字符'o'的位置索引为7。
int strrpos():查找子字符串最后一次出现的位置。
5. 字符串替换操作
str_replace("%body%","blank","..."):用于替换字符串中的指定部分。
6. 大小写敏感性问题说明
Strpos:查找字符串中第一次出现的子字符串位置。
Strrpos:查找某字符继第一次出现之后的最先出现位置(实际通常指最后一次)。
strpos与stripos的区别在于后者不区分大小写;同理,strrpos与strripos。
strstr与stristr的区别在于后者不区分大小写。
str_replace与str_ireplace的区别在于后者不区分大小写。