怎么在php中利用strpos查找字符串的位置

本篇文章为大家展示了怎么在php中利用strpos查找字符串的位置,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

具体分析如下:

strpos用来查找一个字符串在另一个字符串中首次出现的位置,strpos区分大小写,如果没有找到则返回false,所以strpos有两种类型的返回值,一种是整形,一种是bool型,开发过程中需要注意

<?php
echo strpos("Hello world!","wo");
?>

输出结果:6

由于strpos有两种类型的返回值,所以在判断是否找到子字符串的的时候最好使用===三个等号进行严格类型的相等比较

<?php
$haystack = "needle23423432";
$pos = strpos($haystack, "needle");
if ($pos==false) {
 print("Not found based (==) test\n");
} else {
 print("Found based (==) test\n");
}
if ($pos===false) {
 print("Not found based (===) test\n");
} else {
 print("Found based (===) test\n");
}
?>

上面的代码返回如下结果

This script will print:
 
Not found based (==) test
Found based (===) test
 
The (===) test is correct.

上述内容就是怎么在php中利用strpos查找字符串的位置,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。