龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

strpos php通过strpos查找字符串出现位置

时间:2014-10-17 15:41来源:网络整理 作者:网络 点击:
分享到:
php通过strpos查找字符串出现位置 [代码片段(5行)] 结出结果:6
<?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.
精彩图集

赞助商链接