php-doc-en/reference/strings/functions/strpos.xml
2007-12-02 01:08:18 +00:00

144 lines
4 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.15 $ -->
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.strpos">
<refnamediv>
<refname>strpos</refname>
<refpurpose>Find position of first occurrence of a string</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>int</type><methodname>strpos</methodname>
<methodparam><type>string</type><parameter>haystack</parameter></methodparam>
<methodparam><type>mixed</type><parameter>needle</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>offset</parameter></methodparam>
</methodsynopsis>
<para>
Returns the numeric position of the first occurrence of
<parameter>needle</parameter> in the <parameter>haystack</parameter>
string. Unlike the <function>strrpos</function> before PHP 5, this
function can take a full string as the <parameter>needle</parameter>
parameter and the entire string will be used.
</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>
If <parameter>needle</parameter> is not a string, it is converted
to an integer and applied as the ordinal value of a character.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>offset</parameter></term>
<listitem>
<para>
The optional <parameter>offset</parameter> parameter allows you
to specify which character in <parameter>haystack</parameter> to
start searching. The position returned is still relative to the
beginning of <parameter>haystack</parameter>.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the position as an integer. If <parameter>needle</parameter> is
not found, <function>strpos</function> will return <type>boolean</type>
&false;.
</para>
&return.falseproblem;
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>strpos</function> examples</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";
}
// 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>strrpos</function></member>
<member><function>stripos</function></member>
<member><function>strripos</function></member>
<member><function>strrchr</function></member>
<member><function>substr</function></member>
<member><function>stristr</function></member>
<member><function>strstr</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:"../../../../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
-->