php通过strpos查找字符串出现位置的方法_PHP教程
Jul 13, 2016 am 10:03 AMphp通过strpos查找字符串出现位置的方法
这篇文章主要介绍了php通过strpos查找字符串出现位置的方法,实例分析了strpos的功能及使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了php通过strpos查找字符串出现位置的方法。分享给大家供大家参考。具体分析如下:
strpos用来查找一个字符串在另一个字符串中首次出现的位置,strpos区分大小写,如果没有找到则返回false,所以strpos有两种类型的返回值,一种是整形,一种是bool型,开发过程中需要注意
?
1 2 3 |
echo strpos("Hello world!","wo"); ?> |
输出结果:6
由于strpos有两种类型的返回值,所以在判断是否找到子字符串的的时候最好使用===三个等号进行严格类型的相等比较
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$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"); } ?> |
上面的代码返回如下结果
?
1 2 3 4 5 6 |
This script will print:
Not found based (==) test Found based (===) test
The (===) test is correct. |
希望本文所述对大家的php程序设计有所帮助。

Heißer Artikel

Hot-Tools-Tags

Heißer Artikel

Heiße Artikel -Tags

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian

So richten Sie Visual Studio-Code (VS-Code) für die PHP-Entwicklung ein
