<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.strpos" xmlns="http://docbook.org/ns/docbook">
 <refnamediv>
  <refname>strpos</refname>
  <refpurpose>Find the position of the first occurrence of a substring in a string</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type class="union"><type>int</type><type>false</type></type><methodname>strpos</methodname>
   <methodparam><type>string</type><parameter>haystack</parameter></methodparam>
   <methodparam><type>string</type><parameter>needle</parameter></methodparam>
   <methodparam choice="opt"><type>int</type><parameter>offset</parameter><initializer>0</initializer></methodparam>
  </methodsynopsis>
  <para>
   Find the numeric position of the first occurrence of
   <parameter>needle</parameter> in the <parameter>haystack</parameter> string.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term><parameter>haystack</parameter></term>
     <listitem>
      <para>
       The string to search in.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>needle</parameter></term>
     <listitem>
      <para>
       The string to search for.
      </para>
      &strings.parameter.needle.non-string;
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>offset</parameter></term>
     <listitem>
      <para>
       If specified, search will start this number of characters counted from
       the beginning of the string. If the offset is negative, the search will start
       this number of characters counted from the end of the string.
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   Returns the position of where the needle exists relative to the beginning of
   the <parameter>haystack</parameter> string (independent of offset).
   Also note that string positions start at 0, and not 1.
  </para>
  <para>
   Returns &false; if the needle was not found.
  </para>
  &return.falseproblem;
 </refsect1>

 <refsect1 role="changelog">
  &reftitle.changelog;
  <informaltable>
   <tgroup cols="2">
    <thead>
     <row>
      <entry>&Version;</entry>
      <entry>&Description;</entry>
     </row>
    </thead>
    <tbody>
     &strings.changelog.needle-empty;
     <row>
      <entry>8.0.0</entry>
      <entry>
       Passing an &integer; as <parameter>needle</parameter> is no longer supported.
      </entry>
     </row>
     <row>
      <entry>7.3.0</entry>
      <entry>
       Passing an &integer; as <parameter>needle</parameter> has been deprecated.
      </entry>
     </row>
     <row>
      <entry>7.1.0</entry>
      <entry>
       Support for negative <parameter>offset</parameter>s has been added.
      </entry>
     </row>
    </tbody>
   </tgroup>
  </informaltable>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <para>
   <example>
    <title>Using <literal>===</literal></title>
    <programlisting role="php">
<![CDATA[
<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>
]]>
    </programlisting>
   </example>

   <example>
    <title>Using !==</title>
    <programlisting role="php">
<![CDATA[
<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// The !== operator can also be used.  Using != would not work as expected
// because the position of 'a' is 0. The statement (0 != false) evaluates
// to false.
if ($pos !== false) {
     echo "The string '$findme' was found in the string '$mystring'";
         echo " and exists at position $pos";
} else {
     echo "The string '$findme' was not found in the string '$mystring'";
}
?>
]]>
    </programlisting>
   </example>

   <example>
    <title>Using an offset</title>
    <programlisting role="php">
<![CDATA[
<?php
// We can search for the character, ignoring anything before the offset
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0
?>
]]>
    </programlisting>
   </example>
  </para>
 </refsect1>

 <refsect1 role="notes">
  &reftitle.notes;
  &note.bin-safe;
 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><function>stripos</function></member>
    <member><function>str_contains</function></member>
    <member><function>str_ends_with</function></member>
    <member><function>str_starts_with</function></member>
    <member><function>strrpos</function></member>
    <member><function>strripos</function></member>
    <member><function>strstr</function></member>
    <member><function>strpbrk</function></member>
    <member><function>substr</function></member>
    <member><function>preg_match</function></member>
   </simplelist>
  </para>
 </refsect1>

</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
