php-doc-en/reference/strings/functions/strpos.xml
2012-01-18 22:42:27 +00:00

175 lines
4.7 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.strpos">
<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>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><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>
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>
If specified, search will start this number of characters counted from
the beginning of the string. Unlike <function>strrpos</function> and
<function>strripos</function>, the offset cannot be negative.
</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="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>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
-->