📄 function.strpos.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Find position of first occurrence of a string</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.strpbrk.html">strpbrk</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strrchr.html">strrchr</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.strpos" class="refentry"> <div class="refnamediv"> <h1 class="refname">strpos</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">strpos</span> — <span class="dc-title">Find position of first occurrence of a string</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>strpos</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$haystack</tt></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$needle</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$offset</tt></span> ] )</div> <p class="para rdfs-comment"> Returns the numeric position of the first occurrence of <i><tt class="parameter">needle</tt></i> in the <i><tt class="parameter">haystack</tt></i> string. Unlike the <a href="function.strrpos.html" class="function">strrpos()</a> before PHP 5, this function can take a full string as the <i><tt class="parameter">needle</tt></i> parameter and the entire string will be used. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">haystack</tt></i></span> <dd> <p class="para"> The string to search in </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">needle</tt></i></span> <dd> <p class="para"> If <i><tt class="parameter">needle</tt></i> is not a string, it is converted to an integer and applied as the ordinal value of a character. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">offset</tt></i></span> <dd> <p class="para"> The optional <i><tt class="parameter">offset</tt></i> parameter allows you to specify which character in <i><tt class="parameter">haystack</tt></i> to start searching. The position returned is still relative to the beginning of <i><tt class="parameter">haystack</tt></i>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the position as an integer. If <i><tt class="parameter">needle</tt></i> is not found, <b>strpos()</b> will return <a href="language.types.boolean.html" class="type boolean">boolean</a> <b><tt>FALSE</tt></b>. </p> <div class="warning"><b class="warning">Warning</b><p class="simpara">This function mayreturn Boolean <b><tt>FALSE</tt></b>, but may also return a non-Boolean value whichevaluates to <b><tt>FALSE</tt></b>, such as <i>0</i> or"". Please read the section on <a href="language.types.boolean.html" class="link">Booleans</a> for moreinformation. Use <a href="language.operators.comparison.html" class="link">the ===operator</a> for testing the return value of thisfunction.</p></div> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 Using ===</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$mystring </span><span style="color: #007700">= </span><span style="color: #DD0000">'abc'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$findme </span><span style="color: #007700">= </span><span style="color: #DD0000">'a'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pos </span><span style="color: #007700">= </span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$mystring</span><span style="color: #007700">, </span><span style="color: #0000BB">$findme</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Note our use of ===. Simply == would not work as expected<br />// because the position of 'a' was the 0th (first) character.<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$pos </span><span style="color: #007700">=== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"The string '$findme' was not found in the string '$mystring'"</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"The string '$findme' was found in the string '$mystring'"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">" and exists at position $pos"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <div class="example"> <p><b>Example #2 Using !==</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$mystring </span><span style="color: #007700">= </span><span style="color: #DD0000">'abc'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$findme </span><span style="color: #007700">= </span><span style="color: #DD0000">'a'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pos </span><span style="color: #007700">= </span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$mystring</span><span style="color: #007700">, </span><span style="color: #0000BB">$findme</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// The !== operator can also be used. Using != would not work as expected<br />// because the position of 'a' is 0. The statement (0 != false) evaluates <br />// to false.<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$pos </span><span style="color: #007700">!== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"The string '$findme' was found in the string '$mystring'"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">" and exists at position $pos"</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"The string '$findme' was not found in the string '$mystring'"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <div class="example"> <p><b>Example #3 Using an offset</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// We can search for the character, ignoring anything before the offset<br /></span><span style="color: #0000BB">$newstring </span><span style="color: #007700">= </span><span style="color: #DD0000">'abcdef abcdef'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pos </span><span style="color: #007700">= </span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$newstring</span><span style="color: #007700">, </span><span style="color: #DD0000">'a'</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// $pos = 7, not 0<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</b>: <span class="simpara">This function isbinary-safe.</span></p></blockquote> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.strrpos.html" class="function" rel="rdfs-seeAlso">strrpos()</a></li> <li class="member"><a href="function.stripos.html" class="function" rel="rdfs-seeAlso">stripos()</a></li> <li class="member"><a href="function.strripos.html" class="function" rel="rdfs-seeAlso">strripos()</a></li> <li class="member"><a href="function.strrchr.html" class="function" rel="rdfs-seeAlso">strrchr()</a></li> <li class="member"><a href="function.substr.html" class="function" rel="rdfs-seeAlso">substr()</a></li> <li class="member"><a href="function.stristr.html" class="function" rel="rdfs-seeAlso">stristr()</a></li> <li class="member"><a href="function.strstr.html" class="function" rel="rdfs-seeAlso">strstr()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.strpbrk.html">strpbrk</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strrchr.html">strrchr</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -