PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/manual/en/function.strpos.php

https://github.com/ssbalakumar/web-php
PHP | 201 lines | 166 code | 35 blank | 0 comment | 4 complexity | 7c11bb3ab1410cee56552765838c8c7a MD5 | raw file
  1. <?php
  2. include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
  3. $TOC = array();
  4. $PARENTS = array();
  5. include_once dirname(__FILE__) ."/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 => 'function.strpbrk.php',
  30. 1 => 'strpbrk',
  31. ),
  32. 'next' =>
  33. array (
  34. 0 => 'function.strrchr.php',
  35. 1 => 'strrchr',
  36. ),
  37. 'alternatives' =>
  38. array (
  39. ),
  40. );
  41. $setup["toc"] = $TOC;
  42. $setup["parents"] = $PARENTS;
  43. manual_setup($setup);
  44. manual_header();
  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> &mdash; <span class="dc-title">Find the position of the first occurrence of a substring in a string</span></p>
  50. </div>
  51. <div class="refsect1 description" id="refsect1-function.strpos-description">
  52. <h3 class="title">Description</h3>
  53. <div class="methodsynopsis dc-description">
  54. <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>
  55. ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$haystack</code></span>
  56. , <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>
  57. [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code><span class="initializer"> = 0</span></span>
  58. ] )</div>
  59. <p class="para rdfs-comment">
  60. Find the numeric position of the first occurrence of
  61. <em><code class="parameter">needle</code></em> in the <em><code class="parameter">haystack</code></em> string.
  62. </p>
  63. </div>
  64. <div class="refsect1 parameters" id="refsect1-function.strpos-parameters">
  65. <h3 class="title">Parameters</h3>
  66. <p class="para">
  67. <dl>
  68. <dt>
  69. <span class="term"><em><code class="parameter">haystack</code></em></span>
  70. <dd>
  71. <p class="para">
  72. The string to search in.
  73. </p>
  74. </dd>
  75. </dt>
  76. <dt>
  77. <span class="term"><em><code class="parameter">needle</code></em></span>
  78. <dd>
  79. <p class="para">
  80. If <em><code class="parameter">needle</code></em> is not a string, it is converted
  81. to an integer and applied as the ordinal value of a character.
  82. </p>
  83. </dd>
  84. </dt>
  85. <dt>
  86. <span class="term"><em><code class="parameter">offset</code></em></span>
  87. <dd>
  88. <p class="para">
  89. If specified, search will start this number of characters counted from
  90. the beginning of the string. Unlike <span class="function"><a href="function.strrpos.php" class="function">strrpos()</a></span> and
  91. <span class="function"><a href="function.strripos.php" class="function">strripos()</a></span>, the offset cannot be negative.
  92. </p>
  93. </dd>
  94. </dt>
  95. </dl>
  96. </p>
  97. </div>
  98. <div class="refsect1 returnvalues" id="refsect1-function.strpos-returnvalues">
  99. <h3 class="title">Return Values</h3>
  100. <p class="para">
  101. Returns the position of where the needle exists relative to the beginning of
  102. the <em><code class="parameter">haystack</code></em> string (independent of offset).
  103. Also note that string positions start at 0, and not 1.
  104. </p>
  105. <p class="para">
  106. Returns <strong><code>FALSE</code></strong> if the needle was not found.
  107. </p>
  108. <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function may
  109. return Boolean <strong><code>FALSE</code></strong>, but may also return a non-Boolean value which
  110. evaluates to <strong><code>FALSE</code></strong>. Please read the section on <a href="language.types.boolean.php" class="link">Booleans</a> for more
  111. information. Use <a href="language.operators.comparison.php" class="link">the ===
  112. operator</a> for testing the return value of this
  113. function.</p></div>
  114. </div>
  115. <div class="refsect1 examples" id="refsect1-function.strpos-examples">
  116. <h3 class="title">Examples</h3>
  117. <p class="para">
  118. <div class="example" id="example-4886">
  119. <p><strong>Example #1 Using <em>===</em></strong></p>
  120. <div class="example-contents">
  121. <div class="phpcode"><code><span style="color: #000000">
  122. <span style="color: #0000BB">&lt;?php<br />$mystring&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'abc'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$findme&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pos&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$mystring</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$findme</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Note&nbsp;our&nbsp;use&nbsp;of&nbsp;===.&nbsp;&nbsp;Simply&nbsp;==&nbsp;would&nbsp;not&nbsp;work&nbsp;as&nbsp;expected<br />//&nbsp;because&nbsp;the&nbsp;position&nbsp;of&nbsp;'a'&nbsp;was&nbsp;the&nbsp;0th&nbsp;(first)&nbsp;character.<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$pos&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;string&nbsp;'</span><span style="color: #0000BB">$findme</span><span style="color: #DD0000">'&nbsp;was&nbsp;not&nbsp;found&nbsp;in&nbsp;the&nbsp;string&nbsp;'</span><span style="color: #0000BB">$mystring</span><span style="color: #DD0000">'"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;string&nbsp;'</span><span style="color: #0000BB">$findme</span><span style="color: #DD0000">'&nbsp;was&nbsp;found&nbsp;in&nbsp;the&nbsp;string&nbsp;'</span><span style="color: #0000BB">$mystring</span><span style="color: #DD0000">'"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;and&nbsp;exists&nbsp;at&nbsp;position&nbsp;</span><span style="color: #0000BB">$pos</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
  123. </span>
  124. </code></div>
  125. </div>
  126. </div>
  127. <div class="example" id="example-4887">
  128. <p><strong>Example #2 Using !==</strong></p>
  129. <div class="example-contents">
  130. <div class="phpcode"><code><span style="color: #000000">
  131. <span style="color: #0000BB">&lt;?php<br />$mystring&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'abc'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$findme&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pos&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$mystring</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$findme</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;!==&nbsp;operator&nbsp;can&nbsp;also&nbsp;be&nbsp;used.&nbsp;&nbsp;Using&nbsp;!=&nbsp;would&nbsp;not&nbsp;work&nbsp;as&nbsp;expected<br />//&nbsp;because&nbsp;the&nbsp;position&nbsp;of&nbsp;'a'&nbsp;is&nbsp;0.&nbsp;The&nbsp;statement&nbsp;(0&nbsp;!=&nbsp;false)&nbsp;evaluates&nbsp;<br />//&nbsp;to&nbsp;false.<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$pos&nbsp;</span><span style="color: #007700">!==&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;string&nbsp;'</span><span style="color: #0000BB">$findme</span><span style="color: #DD0000">'&nbsp;was&nbsp;found&nbsp;in&nbsp;the&nbsp;string&nbsp;'</span><span style="color: #0000BB">$mystring</span><span style="color: #DD0000">'"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;and&nbsp;exists&nbsp;at&nbsp;position&nbsp;</span><span style="color: #0000BB">$pos</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;string&nbsp;'</span><span style="color: #0000BB">$findme</span><span style="color: #DD0000">'&nbsp;was&nbsp;not&nbsp;found&nbsp;in&nbsp;the&nbsp;string&nbsp;'</span><span style="color: #0000BB">$mystring</span><span style="color: #DD0000">'"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
  132. </span>
  133. </code></div>
  134. </div>
  135. </div>
  136. <div class="example" id="example-4888">
  137. <p><strong>Example #3 Using an offset</strong></p>
  138. <div class="example-contents">
  139. <div class="phpcode"><code><span style="color: #000000">
  140. <span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;We&nbsp;can&nbsp;search&nbsp;for&nbsp;the&nbsp;character,&nbsp;ignoring&nbsp;anything&nbsp;before&nbsp;the&nbsp;offset<br /></span><span style="color: #0000BB">$newstring&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'abcdef&nbsp;abcdef'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pos&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$newstring</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$pos&nbsp;=&nbsp;7,&nbsp;not&nbsp;0<br /></span><span style="color: #0000BB">?&gt;</span>
  141. </span>
  142. </code></div>
  143. </div>
  144. </div>
  145. </p>
  146. </div>
  147. <div class="refsect1 notes" id="refsect1-function.strpos-notes">
  148. <h3 class="title">Notes</h3>
  149. <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function is
  150. binary-safe.</span></p></blockquote>
  151. </div>
  152. <div class="refsect1 seealso" id="refsect1-function.strpos-seealso">
  153. <h3 class="title">See Also</h3>
  154. <p class="para">
  155. <ul class="simplelist">
  156. <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>
  157. <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>
  158. <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>
  159. <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>
  160. <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>
  161. <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>
  162. <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>
  163. </ul>
  164. </p>
  165. </div>
  166. </div><?php manual_footer(); ?>