首页 >后端开发 >php教程 >字符串函数库-搜索类型_PHP

字符串函数库-搜索类型_PHP

WBOY
WBOY原创
2016-06-01 12:38:41961浏览

兼容:(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>

 

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn