Blogger Information
Blog 36
fans 1
comment 0
visits 29751
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP之十个常用字符串函数
Jason
Original
747 people have browsed it

php十个常用字符串函数

在php中,提供了非常强大的字符串函数,学会了这些函数,能够让我们在工作当中,简化很多代码。

1.substr()分割字符串

主要用来从字符串中,分割其中的字符,起始长度为1时,是从左边,为-1时,是从右边
substr($string,$start,$length);
示例

  1. echo substr('hello the world', -5,5) , '<br>';
  2. echo substr('hello the world', 6, 3);

输出:
world
the

2.substr_count():统计子串数量

统计某个子串的出现频率和次数
substr_count($str, $needel, $start, $length)
示例

  1. echo substr_count('this is a test','is'),'<br>';
  2. echo substr_count('hello teacher good','o',5),'<br>';
  3. echo substr_count('this is a test','is',5,9),'<br>';

输出:
2
2
1

3.implode():一维数组转字符串

用指定字符串将数组组装成一个字符串返回

示例

  1. echo implode('***',['html','css','js','php']),'<br>';
  2. echo implode('1',['html','9','99','333']),'<br>';

输出:
html***css***js***php
html191991333

4.explode():分割字符串

使用一个字符串来分割另一个字符串,返回数组

示例

  1. $paras = 'localhost-root-utf8-3306';
  2. printf('<pre>%s</pre>', print_r(explode('-',$paras,4),true));
  3. list($host,$user,$pass) = explode('-',$paras,4);
  4. echo "$host:$user => $pass";
  5. echo '<hr>';

输出

  1. Array
  2. (
  3. [0] => localhost
  4. [1] => root
  5. [2] => utf8
  6. [3] => 3306
  7. )
  8. localhost:root => utf8

5.printf():格式化输出

在输出的字符串中,用一个占位符代替
示例

$site = ‘php.cn’;
//$s:字符串,$d:数值
printf(‘Hello %s’,$site);
echo ‘<br>‘;
printf(‘SELECT * FROM %s LIMIT %d’,’staff’,25);
echo ‘<hr>‘;

  1. 输出
  2. ```php
  3. Hello php.cn
  4. SELECT * FROM `staff` LIMIT 25

6.cout_chars

返回字符串所用字符的信息
示例

  1. $res = '123';
  2. echo count_chars($res),'<br>';

输出
Array

7.strpos()

返回一个整型字符,查找字符串首次出现的位置
示例

  1. $res = 'hello the world';
  2. echo strpos($res,'o'),'<br>';

输出
4

8.ucwords()

单词的首字母大写
示例

  1. $str = 'the gread wall';
  2. echo ucwords($str),'<br>';

输出
The Gread Wall

9.str_replace()子字符串替换

搜索字符串中的值,进行替换
示例1

  1. $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
  2. $onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
  3. echo $onlyconsonants,'<br>';
  4. 输出
  5. ```php
  6. Hll Wrld f PHP

示例2

  1. $search = array('A','B','C','D','E');
  2. $replace = array('B','C','D','E','F');
  3. $subject = 'A';
  4. echo str_replace($search,$replace,$subject),'<br>';

输出

F

示例3

  1. $str = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
  2. echo $str,'<br>';
  3. $order = array("\r\n","\r");
  4. $replace = '<br>';
  5. $newstr = str_replace($order,$replace,$str);
  6. echo $newstr,'<br>';

输出

  1. Line 1 Line 2 Line 3 Line 4
  2. Line 1 Line 2
  3. Line 3
  4. Line 4

进行多重替换时,可能替换掉前面的值

  1. $str = "You should eat fruits,vegetables, and fiber every day.";
  2. $healthy = array('fruits','vegetables','fiber','pizza');
  3. $yummy = array('pizza','beer','ice cream','book');
  4. echo str_replace($healthy,$yummy,$str),'<br>';

输出

You should eat book,beer, and ice cream every day.
The Gread Wall

10.ord()

得到指定字符的ASCII
示例

  1. $str = 'BC';
  2. echo ord($str);

输出
66

总结

字符串函数种类比较多,老师上课已经把经常用到的函数讲了一遍,根据老师的讲解,理解起来很简单,当然还有更多的字符串函数,还需要看手册,了解他们的用法,可能以后用的很少,但是一定要知道,有这么一个东西。

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