字符串函数库-搜索类型_PHP
兼容:(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>

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

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

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

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

热门话题

Go语言提供了两种动态函数创建技术:closures和反射。closures允许访问闭包作用域内的变量,而反射可使用FuncOf函数创建新函数。这些技术在自定义HTTP路由器、实现高度可定制的系统和构建可插拔的组件方面非常有用。

在C++函数命名中,考虑参数顺序至关重要,可提高可读性、减少错误并促进重构。常见的参数顺序约定包括:动作-对象、对象-动作、语义意义和遵循标准库。最佳顺序取决于函数目的、参数类型、潜在混淆和语言惯例。

1、 SUM函数,用于对一列或一组单元格中的数字进行求和,例如:=SUM(A1:J10)。2、AVERAGE函数,用于计算一列或一组单元格中的数字的平均值,例如:=AVERAGE(A1:A10)。3、COUNT函数,用于计算一列或一组单元格中的数字或文本的数量,例如:=COUNT(A1:A10)4、IF函数,用于根据指定的条件进行逻辑判断,并返回相应的结果。

C++函数中默认参数的优点包括简化调用、增强可读性、避免错误。缺点是限制灵活性、命名限制。可变参数的优点包括无限灵活性、动态绑定。缺点包括复杂性更高、隐式类型转换、调试困难。

自定义PHP函数与预定义函数的区别在于:作用域:自定义函数仅限于其定义范围,而预定义函数可在整个脚本中访问。定义方式:自定义函数使用function关键字定义,而预定义函数由PHP内核定义。参数传递:自定义函数接收参数,而预定义函数可能不需要参数。扩展性:自定义函数可以根据需要创建,而预定义函数是内置的且无法修改。

C++中的异常处理可通过定制异常类增强,提供特定错误消息、上下文信息以及根据错误类型执行自定义操作。定义继承自std::exception的异常类,提供特定的错误信息。使用throw关键字抛出定制异常。在try-catch块中使用dynamic_cast将捕获到的异常转换为定制异常类型。实战案例中,open_file函数抛出FileNotFoundException异常,捕捉并处理该异常可提供更具体的错误消息。

C++函数中的引用参数(本质为变量别名,修改引用修改原始变量)和指针参数(存储原始变量的内存地址,通过解引用指针修改变量)在传递和修改变量时有着不同的用法。引用参数常用于修改原始变量(尤其是大型结构体),传递给构造函数或赋值运算符时避免复制开销。指针参数则用于灵活指向内存位置,实现动态数据结构或传递空指针表示可选参数。
