兼容:(PHP 3, PHP 4, PHP 5)
strpos -- Find position of first occurrence of a string
查找字符在字符串第一次出现的位置
语法:int strpos ( string haystack, mixed needle [, int offset] )
返回值:整数
函数种类: 资料处理
内容说明:
英文:
Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos(), this function can take a full string as the needle parameter and the entire string will be used.
If needle is not found, strpos() will return boolean FALSE.
If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack.
中文:
传回参数 needle在字串 haystack中第一次出现的位置,以数字表示。不像strrpos( ),此函式可以取参数 needle全部的字串,而且会使用全部的字串。如果找不到参数 needle,则传回 false。
如果参数 needle不是字串时,它会转换成整数并且按照字元的顺序值来使用。
参数 offset允许你去指定在 haystack中从那一个字元开始搜寻,传回的位置依然是相对於 haystack的起点。
*值得注意的是 needle 只能是一个字符,中文字等就不适合了。
例子讲解:<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br>$mystring </font><font color="#007700">= </font><font color="#DD0000">'abc'</font><font color="#007700">;<br><br></font><font color="#0000BB">$findme</font><font color="#007700">= </font><font color="#DD0000">'a'</font><font color="#007700">;<br><br></font><font color="#0000BB">$pos </font><font color="#007700">= </font><font color="#0000BB">strpos</font><font color="#007700">(</font><font color="#0000BB">$mystring</font><font color="#007700">, </font><font color="#0000BB">$findme</font><font color="#007700">);<br><br><br><br></font><font color="#FF8000">// Note our use of ===.Simply == would not work as expected<br><br>// because the position of 'a' was the 0th (first) character.<br><br></font><font color="#007700">if (</font><font color="#0000BB">$pos </font><font color="#007700">=== </font><font color="#0000BB">false</font><font color="#007700">) {<br><br>echo </font><font color="#DD0000">"The string '$findme' was not found in the string '$mystring'\";<br><br>} else {<br><br>echo \"The string '$findme' was found in the string '$mystring'\";<br><br>echo \" and exists at position $pos\";<br><br>}<br><br><br><br>// We can search for the character, ignoring anything before the offset<br><br>$newstring = 'abcdef abcdef';<br><br>$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0<br><br>?></font>
</font>
strpos()与substr_count()对比:<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br>$mystring </font><font color="#007700">= </font><font color="#DD0000">"Hello Chris\";<br><br>if (substr_count($mystring, \"Hello\") == 0)<br><br>echo \"no\";<br><br>// same as:<br><br>if (strpos($mystring, \"Hello\") === false)<br><br>echo \"no\";<br><br>?></font>
</font>
对比下面两个代码注意数字情况下,容易出现的错误,数字整数情况下不能看成字符串。<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br>$val1</font><font color="#007700">=</font><font color="#0000BB">123</font><font color="#007700">;<br><br></font><font color="#0000BB">$val2</font><font color="#007700">=</font><font color="#DD0000">"123,456,789\";<br><br>if (strpos($val2, $val1)!==false) echo \"matched\";<br><br>else echo \"not matched\";<br><br>?></font>
</font>
<font color="#000000">
<font color="#0000BB">结果为: not matched</font>
</font>
<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br>$val1</font><font color="#007700">=(string)</font><font color="#0000BB">123</font><font color="#007700">;<br><br></font><font color="#0000BB">$val2</font><font color="#007700">=</font><font color="#DD0000">"123,456,789\";<br><br>if (strpos($val2, $val1)!==false) echo \"matched\";<br><br>else echo \"not matched\";<br><br>?></font>
</font>
<font color="#000000">
<font color="#0000BB">结果为:not matched</font>
</font>

设置无线网络很常见,但选择或更改网络类型可能会令人困惑,尤其是在您不知道后果的情况下。如果您正在寻找有关如何在Windows11中将网络类型从公共更改为私有或反之亦然的建议,请继续阅读以获取一些有用的信息。Windows11中有哪些不同的网络配置文件?Windows11附带了许多网络配置文件,这些配置文件本质上是可用于配置各种网络连接的设置集。如果您在家中或办公室有多个连接,这将非常有用,因此您不必每次连接到新网络时都进行所有设置。专用和公用网络配置文件是Windows11中的两种常见类型,但通

Part1聊聊Python序列类型的本质在本博客中,我们来聊聊探讨Python的各种“序列”类,内置的三大常用数据结构——列表类(list)、元组类(tuple)和字符串类(str)的本质。不知道你发现没有,这些类都有一个很明显的共性,都可以用来保存多个数据元素,最主要的功能是:每个类都支持下标(索引)访问该序列的元素,比如使用语法Seq[i]。其实上面每个类都是使用数组这种简单的数据结构表示。但是熟悉Python的读者可能知道这3种数据结构又有一些不同:比如元组和字符串是不能修改的,列表可以

随着短视频平台的盛行,视频矩阵账号营销已成为一种新兴营销方式。通过在不同平台上创建和管理多个账号,企业和个人能够实现品牌推广、粉丝增长和产品销售等目标。本文将为您探讨如何有效运用视频矩阵账号,并介绍不同类型的视频矩阵账号。一、视频矩阵账号怎么做?要想做好视频矩阵账号,需要遵循以下几个步骤:首先要明确你的视频矩阵账号的目标是什么,是为了品牌传播、粉丝增长还是产品销售。明确目标有助于制定相应的策略。2.选择平台:根据你的目标受众,选择合适的短视频平台。目前主流的短视频平台有抖音、快手、火山小视频等。

Go函数可以返回多个不同类型的值,返回值类型在函数签名中指定,并通过return语句返回。例如,函数可以返回一个整数和一个字符串:funcgetDetails()(int,string)。实战中,一个计算圆面积的函数可以返回面积和一个可选错误:funccircleArea(radiusfloat64)(float64,error)。注意事项:如果函数签名未指定类型,则返回空值;建议使用显式类型声明的return语句以提高可读性。

使用动态语言一时爽,代码重构火葬场。相信你一定听过这句话,和单元测试一样,虽然写代码的时候花费你少量的时间,但是从长远来看,这是非常值得的。本文分享如何更好的理解和使用Python的类型提示。1、类型提示仅在语法层面有效类型提示(自PEP3107开始引入)用于向变量、参数、函数参数以及它们的返回值、类属性和方法添加类型。Python的变量类型是动态的,可以在运行时修改,为代码添加类型提示,仅在语法层面支持,对代码的运行没有任何影响,Python解释器在运行代码的时候会忽略类型提示。因此类型提

C++函数有以下类型:简单函数、const函数、静态函数、虚函数;特性包括:inline函数、默认参数、引用返回、重载函数。例如,calculateArea函数使用π计算给定半径圆的面积,并将其作为输出返回。

Java注解用于为代码元素提供元数据,可用于元编程、错误检查、代码生成、文档生成和反射,其中Spring框架广泛使用注解进行配置,简化了应用程序开发。

解析PHP8的新特性:如何利用属性的更强类型?近年来,PHP在Web开发领域越来越受欢迎。它的灵活性和易学性使得PHP成为一种流行的编程语言,适用于各种项目。为了不断改进和优化语言,PHP8引入了许多新特性,其中包括属性的更强类型。在本文中,我们将深入探讨PHP8中属性类型的新特性,以及如何利用它们来提高代码的可靠性和性能。在PHP8之前,属性的类型声明可通


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。