1<?php 2include_once __DIR__ . '/../../include/shared-manual.inc'; 3$TOC = array(); 4$PARENTS = array(); 5include_once __DIR__ ."/toc/ref.strings.inc"; 6$setup = array ( 7 'home' => 8 array ( 9 0 => 'index.php', 10 1 => 'PHP Manual', 11 ), 12 'head' => 13 array ( 14 0 => 'UTF-8', 15 1 => 'en', 16 ), 17 'this' => 18 array ( 19 0 => 'function.strpos.php', 20 1 => 'strpos', 21 ), 22 'up' => 23 array ( 24 0 => 'ref.strings.php', 25 1 => 'String Functions', 26 ), 27 'prev' => 28 array ( 29 0 => 'ref.strings.php', 30 1 => 'String Functions', 31 ), 32 'next' => 33 array ( 34 0 => 'function.rtrim.php', 35 1 => 'rtrim', 36 ), 37 'alternatives' => 38 array ( 39 ), 40); 41$setup["toc"] = $TOC; 42$setup["parents"] = $PARENTS; 43manual_setup($setup); 44 45?> 46<div id="function.strpos" class="refentry"> 47 <div class="refnamediv"> 48 <h1 class="refname">strpos</h1> 49 <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">strpos</span> — <span class="dc-title">Find the position of the first occurrence of a substring in a string</span></p> 50 51 </div> 52 53 <div class="refsect1 description" id="refsect1-function.strpos-description"> 54 <h3 class="title">Description</h3> 55 <div class="methodsynopsis dc-description"> 56 <span class="type"><a href="language.pseudo-types.php#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>strpos</strong></span> 57 ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$haystack</code></span> 58 , <span class="methodparam"><span class="type"><a href="language.pseudo-types.php#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$needle</code></span> 59 [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code><span class="initializer"> = 0</span></span> 60 ] )</div> 61 62 <p class="para rdfs-comment"> 63 Find the numeric position of the first occurrence of 64 <em><code class="parameter">needle</code></em> in the <em><code class="parameter">haystack</code></em> string. 65 </p> 66 </div> 67 68 69 <div class="refsect1 parameters" id="refsect1-function.strpos-parameters"> 70 <h3 class="title">Parameters</h3> 71 <p class="para"> 72 <dl> 73 74 <dt> 75 76 <span class="term"><em><code class="parameter">haystack</code></em></span> 77 <dd> 78 79 <p class="para"> 80 The string to search in. 81 </p> 82 </dd> 83 84 </dt> 85 86 <dt> 87 88 <span class="term"><em><code class="parameter">needle</code></em></span> 89 <dd> 90 91 <p class="para"> 92 If <em><code class="parameter">needle</code></em> is not a string, it is converted 93 to an integer and applied as the ordinal value of a character. 94 </p> 95 </dd> 96 97 </dt> 98 99 <dt> 100 101 <span class="term"><em><code class="parameter">offset</code></em></span> 102 <dd> 103 104 <p class="para"> 105 If specified, search will start this number of characters counted from 106 the beginning of the string. Unlike <span class="function"><a href="function.strrpos.php" class="function">strrpos()</a></span> and 107 <span class="function"><a href="function.strripos.php" class="function">strripos()</a></span>, the offset cannot be negative. 108 </p> 109 </dd> 110 111 </dt> 112 113 </dl> 114 115 </p> 116 </div> 117 118 119 <div class="refsect1 returnvalues" id="refsect1-function.strpos-returnvalues"> 120 <h3 class="title">Return Values</h3> 121 <p class="para"> 122 Returns the position of where the needle exists relative to the beginning of 123 the <em><code class="parameter">haystack</code></em> string (independent of offset). 124 Also note that string positions start at 0, and not 1. 125 </p> 126 <p class="para"> 127 Returns <strong><code>FALSE</code></strong> if the needle was not found. 128 </p> 129 <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function may 130return Boolean <strong><code>FALSE</code></strong>, but may also return a non-Boolean value which 131evaluates to <strong><code>FALSE</code></strong>. Please read the section on <a href="language.types.boolean.php" class="link">Booleans</a> for more 132information. Use <a href="language.operators.comparison.php" class="link">the === 133operator</a> for testing the return value of this 134function.</p></div> 135 </div> 136 137 138 <div class="refsect1 examples" id="refsect1-function.strpos-examples"> 139 <h3 class="title">Examples</h3> 140 <p class="para"> 141 <div class="example" id="example-4886"> 142 <p><strong>Example #1 Using <em>===</em></strong></p> 143 <div class="example-contents"> 144<div class="phpcode"><code><span style="color: #000000"> 145<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 '</span><span style="color: #0000BB">$findme</span><span style="color: #DD0000">' was not found in the string '</span><span style="color: #0000BB">$mystring</span><span style="color: #DD0000">'"</span><span style="color: #007700">;<br>} else {<br> echo </span><span style="color: #DD0000">"The string '</span><span style="color: #0000BB">$findme</span><span style="color: #DD0000">' was found in the string '</span><span style="color: #0000BB">$mystring</span><span style="color: #DD0000">'"</span><span style="color: #007700">;<br> echo </span><span style="color: #DD0000">" and exists at position </span><span style="color: #0000BB">$pos</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br>}<br></span><span style="color: #0000BB">?></span> 146</span> 147</code></div> 148 </div> 149 150 </div> 151 152 <div class="example" id="example-4887"> 153 <p><strong>Example #2 Using !==</strong></p> 154 <div class="example-contents"> 155<div class="phpcode"><code><span style="color: #000000"> 156<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 '</span><span style="color: #0000BB">$findme</span><span style="color: #DD0000">' was found in the string '</span><span style="color: #0000BB">$mystring</span><span style="color: #DD0000">'"</span><span style="color: #007700">;<br> echo </span><span style="color: #DD0000">" and exists at position </span><span style="color: #0000BB">$pos</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br>} else {<br> echo </span><span style="color: #DD0000">"The string '</span><span style="color: #0000BB">$findme</span><span style="color: #DD0000">' was not found in the string '</span><span style="color: #0000BB">$mystring</span><span style="color: #DD0000">'"</span><span style="color: #007700">;<br>}<br></span><span style="color: #0000BB">?></span> 157</span> 158</code></div> 159 </div> 160 161 </div> 162 163 <div class="example" id="example-4888"> 164 <p><strong>Example #3 Using an offset</strong></p> 165 <div class="example-contents"> 166<div class="phpcode"><code><span style="color: #000000"> 167<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> 168</span> 169</code></div> 170 </div> 171 172 </div> 173 </p> 174 </div> 175 176 177 <div class="refsect1 notes" id="refsect1-function.strpos-notes"> 178 <h3 class="title">Notes</h3> 179 <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function is 180binary-safe.</span></p></blockquote> 181 </div> 182 183 184 <div class="refsect1 seealso" id="refsect1-function.strpos-seealso"> 185 <h3 class="title">See Also</h3> 186 <p class="para"> 187 <ul class="simplelist"> 188 <li class="member"> <span class="function"><a href="function.stripos.php" class="function" rel="rdfs-seeAlso">stripos()</a> - Find the position of the first occurrence of a case-insensitive substring in a string</span></li> 189 <li class="member"> <span class="function"><a href="function.strrpos.php" class="function" rel="rdfs-seeAlso">strrpos()</a> - Find the position of the last occurrence of a substring in a string</span></li> 190 <li class="member"> <span class="function"><a href="function.strripos.php" class="function" rel="rdfs-seeAlso">strripos()</a> - Find the position of the last occurrence of a case-insensitive substring in a string</span></li> 191 <li class="member"> <span class="function"><a href="function.strstr.php" class="function" rel="rdfs-seeAlso">strstr()</a> - Find the first occurrence of a string</span></li> 192 <li class="member"> <span class="function"><a href="function.strpbrk.php" class="function" rel="rdfs-seeAlso">strpbrk()</a> - Search a string for any of a set of characters</span></li> 193 <li class="member"> <span class="function"><a href="function.substr.php" class="function" rel="rdfs-seeAlso">substr()</a> - Return part of a string</span></li> 194 <li class="member"> <span class="function"><a href="function.preg-match.php" class="function" rel="rdfs-seeAlso">preg_match()</a> - Perform a regular expression match</span></li> 195 </ul> 196 </p> 197 </div> 198 199 200</div><?php manual_footer([]); ?> 201