Blogger Information
Blog 43
fans 1
comment 0
visits 33484
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP中几个常用的字符串函数
蔚蓝世纪
Original
686 people have browsed it

一、什么是字符串函数

字符串变量用于存储并处理文本。字符串变量是一个量,而字符串函数是一个集合。字符串、数组和数据库是我们函数里面最常用的三类函数。PHP 字符串函数是 PHP 核心的组成部分。无需安装即可使用这些函数。

二、创建字符串的四种方式

  1. 单引号 | 全部内容视为纯文本 |
  2. 双引号 | 可解析变量和转义特殊字符 |
  3. heredoc | 与双引号类似 |
  4. nowdoc | 与单引号类似|
代码举例:
  1. echo 'I like \n dogs.';
  2. echo '<hr>';
  3. $str = '我们的祖国是花园';
  4. echo "I like {$str} dogs.";
  5. echo '<hr>';
  6. echo <<<DOC
  7. <h4>这是一个字符串</h4>
  8. <h4>这是一个字符串</h4>
  9. DOC;
  10. echo '<hr>';
  11. $y = 'name';
  12. $z = <<<BOB
  13. "abc"$y
  14. <h4>这是一个字符串</h4>
  15. BOB;
  16. echo $z;
  17. echo '<hr>';
  18. echo <<<'CDC'
  19. This is my book.
  20. CDC;
效果:

三、十个常用的字符串函数

字符串函数有很多,这里仅挑选十个常用的字符串函数做练习。

1.trim()函数

作用:删除字符串两端的空格或其他预定义字符。
语法:trim(string,charlist);

代码举例:
  1. $a = ' The girl is my sister. ';
  2. echo $a . '(没有经过trim处理)' ;
  3. echo '<br>';
  4. $b = trim($a);
  5. echo $b . '(经过trim处理)' ;
效果:在查看源代码的时候能看到经过trim处理的字符串两端没有空格。

2.str_repeat()函数

作用:重复使用指定字符串。
语法:str_repeat(string,repeat);

代码举例:
  1. $x = 'The Great Wall!';
  2. $y = str_repeat($x,5);//5表示重复的次数
  3. echo $y;
效果:

3.str_pad()函数

作用:把字符串填充到指定长度。
语法:str_pad(string,length,pad_string,pad_type);

代码举例:
  1. $a = 'There are many tomatoes.';
  2. $b = str_pad($a, 30, '#');//未设置填充字符串位置,默认为右侧
  3. echo $b;
  4. echo '<br>';
  5. $a = 'There are many tomatoes.';
  6. $b = str_pad($a, 30, '*', STR_PAD_LEFT);//设置填充字符串的位置为左侧
  7. echo $b;
效果:

4.strpos()函数

作用:寻找字符串中某字符最先出现的位置。
语法:strpos(string,find,start);

代码举例:
  1. echo strpos('Mary comes from Canada.','c') ;
  2. echo '<br>';
  3. echo strpos('Mary comes from Canada.','m') ;
效果:strpos() 函数对大小写敏感。

5.str_word_count()函数

作用:计算字符串中的单词数。
语法:str_word_count(string,return,char);

代码举例:
  1. echo str_word_count('The world is so beautiful!');
  2. echo '<br>';
  3. echo str_word_count('我 和 我 的 祖 国!');//不能计算中文字符
效果:

6.next()函数

作用:将内部指针指向数组中的下一个元素,并输出。
语法:next(array);

代码举例:
  1. $city = array('北京','上海','天津','杭州','深圳');
  2. echo next($city);
  3. echo next($city);
  4. echo next($city);
效果:如果移动指针的结果超出了数组单元的末端,则 next() 返回 FALSE。

7.str_replace()函数

作用:字符串替换操作,区分大小写。
语法:str_replace(find,replace,string,count);

代码举例:
  1. $a = 'I like dogs.<br>';
  2. echo $a;
  3. echo str_replace('dog','cat',$a);
效果:

8.ucfirst()函数

作用:字符串首字母大写。
语法:ucfirst(string);

代码举例:
  1. $a = 'i can climb the tree.';
  2. $b = ucfirst($a);
  3. echo $b;
  4. echo '<br>';
  5. echo ucfirst('we will go home on Sunday.');
效果:

9.strlen ()函数

作用:返回字符串的长度。
语法:strlen(string);

代码举例:
  1. echo strlen('Those are mushrooms.');
效果://返回的字符串长度包括空格和标点符号

10.strrev()函数

作用:反转字符串。
语法:strrev(string);

代码举例:
  1. $a = 'How many birds are there?';
  2. $b = strrev($a);
  3. echo $b;
效果:

总结

1.字符串函数内容真的很多呀,一定要在实践中记忆,否则会陷入记了忘,忘了记的死循环。
2.编写网站的后台脚本,就如同组装一台机器,只有吃透了每个零部件是起什么作用的,才能更好地组装一台机器。但是要分清楚主次,常用的做重点练习,不常用的仅做了解就好。
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:是不是一开始认以为字符串很简单吧, 没有想到 这里面有这么知识?
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!