PHP 处理和查找字符串

本文最后更新于:2024年3月18日 凌晨

PHP 处理和查找字符串

PHP有许多函数用于操作字符串,查找和修改字符串最常用的函数是用正则表达式来描述字符串的函数,本节介绍的函数没有使用正则表达式,它们比正则表达式快,但是它们只能用于查找确定的字符串(例如,你在查找12/11/01,而不是任何用斜杠分隔的数字)

子串

  • 你想知道在一个较大字符串中你感兴趣的数据的位置,可以使用substr()函数把它复制出来:
1
$piece = substr(string,start [, length]);
  • start参数是在string中要开始复制的位置,如果是0的话,就是说从字符串开头开始复制,length参数是要复制字符的个数(默认是复制到字符串末尾),例如:
1
2
3
$name = "Fred Flintstone";
$fluff = substr($name,6,4); //$fluff是"lint"
$sound = substr($name,11); //$sound是"tone"
  • 要知道一个小字符串在一个大字符串中出现的次数,可以使用 substr_count()函数:
1
$number = substr_count(big_string,small_string);
  • 例如:
1
2
3
4
5
6
7
8
9
10
$sketch = <<< End_of_Sketch
Well,there's egg and bacon;egg sausage and bacon;egg and spam;
egg bacon and spam;egg bacon sausage and spam;dpam bacon sausage
and spam;spam egg spam spam bacon and spam;spam sausage spam
bacon spam tomato and spam;
End_of_Sketch;
$count = substr_count($sketch,"spam");
print("The word spam occurs $count times.");

The word spam occurs 14 times.
  • substr_replace()函数允许使用不同方式对字符串进行修改:
1
$string = substr_replace(original,new,start[,length]);
  • 函数通过start(0的意思是字符串的开头)和length指定的值,用字符串new替换original的一部分,如果没有给出第四个参数,substr_replace()删除从start到字符串末尾的文字。
1
2
$greeting = "good morning citizen";
$farewell = substr_replace($greeting,"bye",5,7); //$farewell为"good bye citizen"
  • 设定length为0来实现无删除的插入:
1
$farewell = substr_replace($greeting,"kind",9,0);	//$farewell为"good bye kind citizen"
  • 设定new为""来实现无插入的删除:
1
$farewell = substr_replace($farewell,"",8);	//$farewell为"good bye"
  • 下面是说明如何在字符串的开头插入内容:
1
$farewell = substr_replace($farewell,"now it's time to say",0,0);	//$farewell为"now it's time to say good bye"
  • 如果start为负值,则指定从字符串末尾开始到字符串开头替换的字符数:
1
$farewell = substr_replace($farewell,"riddance",-3);	//$farewell为"now it's time to say good riddance"
  • 如果length为负值,则指定从字符串末尾开始删除的字符个数:
1
$farewell = substr_replace($farewell,"",-8,-5);	//$farewell为"now it's time to say good dance"

各种字符串函数

  • strrev()函数接收一个字符串然后返回一个翻转顺序的拷贝:
1
$string = strrev(string);
  • 例如:
1
2
3
echo strrev("There is no cabal");

labac on si erehT
  • str_repeat()函数接收一个字符串和一个计数参数,然后返回一个由参数string重复count次组成的新字符串:
1
$repeated = str_repeat(string,count);
  • 例如,创建一个分割线:
1
echo str_repeat('-',40);
  • strpad()函数用另一个字符串填充一个字符串,可以选择用什么样的字符串来填充,以及加在左边,右边或两边都加:
1
$padded = str_pad(to_pad,length[,with[,pad_type]]);
  • 默认是以空格加在字符串右边:
1
2
3
4
$string = str_pad('Fred Flintstone',30);
echo "$string:35:Wilma";

Fred Flintstone :35:Wilma
  • 可选的第三个参数是要增加的字符串:
1
2
3
4
$string = str_pad('Fred Flintstone',30,'. ');
echo "{$string}35";

Fred Flintstone. . . . . . . . . . . :35
  • 可选的第四个参数可以是STR_PAD_RIGHT(默认),STR_PAD_LEFT或STR_PAD_BOTH(左右都加,长度均分,使原字符串居中对齐),例如:
1
2
3
4
5
echo '[' . strpad('Fred Flintstone',30,' ',STR_PAD_LEFT) . "]\n";
echo '[' . strpad('Fred Flintstone',30,' ',STR_PAD_BOTH) . "]\n";

[ Fred Flintstone]
[ Fred Flintston ]

分解字符串

  • PHP提供几个函数来将字符串分解为更小的部分,按复杂性递增排序,它们是explode(),strtok()sscanf()

分解和合并

  • 数据常常以字符串的形式出现,同时必须要分解成一组值,例如,你可能想要在字符串中拆分以逗号分隔的字段,例如"Fred,25,Wilma",在这些情况下,可以使用explode()函数:
1
$array = explode(separator,string[,limit]);
  • 第一个参数separator是包含字段分隔符的字符串第二个参数string是要拆分的字符串,可选的第三个参数limit是要返回数组中值的最大数目,如果达到上限的话,数组的最后一个元素会包含字符串剩余的部分:
1
2
3
$input = 'Pred,25,Wilma';
$fields = explode(',',$input); //$fields为array('Fred','25','Wilma')
$fields = explore(',',$input,2); //$fields为array('Fred','25,Wilma')
  • implode()函数正好和explore()相反–它用含有几个较小字符串的数组创建一个大字符串:
1
$string = implode(separator,array);
  • 第一个参数separator是要放在第二个参数array元素间的字符串,要重建这个以逗号分隔的字符串,只需这样做:
1
2
$fields = array('Fred','25','Wilma');
$string = implode(',',$fields); //$string是'Fred,25,Wilma'
  • join()函数是implode()的别名函数。

Tokenizing标记

  • strtok()函数可用于遍历一个字符串,每次得到一个新的字符串块(标记,token),第一次调用strtok()的时候,需要传递两个参数:要遍历的字符串和标记分隔符:
1
$first_chunk = strtok(string,separator);
  • 要得到剩下的标记,重复调用仅带有分隔符的strtok():
1
$next_chunk = strtok(separator);
  • 例如,可以这样调用:
1
2
3
4
5
6
7
8
9
10
11
$string = "Fred,Flintstone,35,Wilma";
$token = strtok($string,",");
while($token != false){
echo("$token<br>");
$token = strtok(",");
}

Fred
Flintstone
35
Wilma
  • 当没有任何标记可以返回时,strtok()函数返回false
  • 如果再次调用strtok()时带有两个参数,就会重新初始化迭代器,这样就可以从字符串的开头重新开始获取子字符串。

sscanf()

  • sscant()函数依照和printf()相似的模板来分解一个字符串:
1
2
$array = sscanf(string,template);
$count = sscanf(string,template,var1,...);
  • 如果没有使用可选变量的话,sscanf()返回一个字段数组:
1
2
3
4
5
6
7
8
$string = "Fred\tFlintstone(35)";
$a = sscanf($string,"%s\t%s(%d)");
print_r($a);Array
(
[0] => Fred
[1] => Flintstone
[2] => 35
)
  • 传递引用给变量,可以将字段存放在这些变量总中,将返回字符的个数:
1
2
3
4
5
$string = "Fred\tFlintstone(35)";
$n = sscanf($string,"%s\t%s(%d)",&$first,&$last,&$age);
echo "Matched $n fields: $first $last is $age years old";

Matched 3 fields: Fred Flintstone is 35 years old

字符串查找函数

  • 有一些函数用于在一个较大字符串中查找字符串或字符,它们分为3个系列:strpos()strrpos()返回一个位置,strstr(),strchr()等返回找到的字符串,strspn()strcspn()返回字符串的开头有多少与掩码匹配。
  • 字符的ASCII顺序值来查找,因此,下面的函数调用是相等的(因为44是逗号的ASCII值):
1
2
$pos = strpos($large,",")	// 查找第一个逗号。
$pos = strpos($large,",") // 也查找第一个逗号。
  • 如果没有找到指定的子串,所有的字符串查找函数都会返回flase,如果子串出现在字符串的开头,函数就返回0,因为false可以转换为数字0,所以在测试失败时使用===来比较返回值:
1
2
3
4
5
if($pos == false){
// 没有找到子串。
}else{
// 找到子串,$pos是字符串的偏移量。
}

返回位置的查找

  • strpos()函数查找一个小字符串在较大字符串中第一次出现的位置:
1
$positon = strpos(large_string,small_string);
  • 如果没有找到小字符串,strpos()返回false
  • strrpos()函数查找某个字符(注意是单个字符而不是字符串)在字符串中最后一次出现的位置,它的参数及返回值类型和strpos()相同。
  • 例如:
1
2
3
4
5
$record() = "Fred,Flintstone,35,Wilma";
$pos = strrpos($record,",");// 查找最后一个逗号。
echo ("The last comma in the record is at position $pos");

The last comma in the record is at position 18
  • 如果将一个字符串作为第二个参数传递给strrpos(),只查找参数字符串的首字符。
  • 那么如何查找一个字符串的最后出现位置呢? 请看下面的例子,先将字符串进行翻转并使用strpos():
1
2
3
4
5
6
7
8
9
10
11
12
13
$long = "Today is the we go on holiday to Flirida";
$to_find = "day";
$pos = strpos(strrev($long),strrev($to_find));
if($pos === false){
echo("Not found");
}else{
//$pos是在反转字符串的中的位置。
// 准换为正常的字符串中的位置。
$pos = strlen($long) - $pos - strlen($to_find);
echo ("Last occurrence starts at position $pos");
}

Last occurrence starts at position 30

返回剩余字符串的查找

  • strstr()函数查找一个小字符串一个小字符串在大字符串中第一次出现的位置,并返回从小字符串开始的部分,例如:
1
2
$record = "Fred,Flintstone,35,Wilma";
$rest = strstr($record,","); //$rest为:",Flintstone,35,Wilma"
  • strstr()的变种有:
1
stristr()
  • 不区分大小写的strstr()
1
strchr()
  • strstr()的别名。
1
strrchr()
  • 查找字符在字符串中最后出现的位置。
  • strrpos()一样,strrchr()从后查找字符串,但是都只对字符,而不是一整个字符串。

使用掩码查找

  • 如果认为strrchr()深奥,那么你还没有真正理解它,strspn()strcspn()函数告诉你字符串开头有多少个指定的字符:
1
$length = strspn(string,charset);
  • 例如,下面的函数测试一个字符串是否包含一个八进制数字:
1
2
3
function is_octal($str){
return strspn($str,'01234567') == strlen($str);
}
  • strcspn()里的c代表complement(补足物),它说明字符串的开头有多少不是由字符集中的字符组成,当感兴趣的字符比不感兴趣的字符多时,使用strcspn(),例如,下面的函数测试一个字符串中是否有任何空字符,制表符或回车:
1
2
3
function has_bad_chars($str){
return strcspn($str,"\n\t\0") == strlen($str);
}
  • 分解URL
  • parse_url()函数返回一个由URL成分组成的数组:
1
$array = parse_url(url);
  • 例如:
1
2
3
4
5
6
7
8
9
10
$bits = parse_url('http://me:secret@example.com/cgi-bin/board?user=fred');
print_r($bits);
Array
(
[scheme] => http
[host] => example.com
[user] =>me
[pass] => /cgi-bin/board
[query] => user=fred
)
  • 得到的数组键名可能为scheme,host,port,user,path,query和fragment

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!