Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php通过strpos查找字符串出现位置的方法,strpos字符串_PHP教程

php通过strpos查找字符串出现位置的方法,strpos字符串_PHP教程

WBOY
WBOYasal
2016-07-13 10:02:481466semak imbas

php通过strpos查找字符串出现位置的方法,strpos字符串

本文实例讲述了php通过strpos查找字符串出现位置的方法。分享给大家供大家参考。具体分析如下:

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

<&#63;php
echo strpos("Hello world!","wo");
&#63;>

输出结果:6

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

<&#63;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");
}
&#63;>

上面的代码返回如下结果

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

希望本文所述对大家的php程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/969501.htmlTechArticlephp通过strpos查找字符串出现位置的方法,strpos字符串 本文实例讲述了php通过strpos查找字符串出现位置的方法。分享给大家供大家参考。具体...
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn