<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.substr-compare">
 <refnamediv>
  <refname>substr_compare</refname>
  <refpurpose>Binary safe comparison of two strings from an offset, up to length characters</refpurpose>
 </refnamediv>
 
 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type>int</type><methodname>substr_compare</methodname>
   <methodparam><type>string</type><parameter>main_str</parameter></methodparam>
   <methodparam><type>string</type><parameter>str</parameter></methodparam>
   <methodparam><type>int</type><parameter>offset</parameter></methodparam>
   <methodparam choice="opt"><type>int</type><parameter>length</parameter></methodparam>
   <methodparam choice="opt"><type>bool</type><parameter>case_insensitivity</parameter><initializer>&false;</initializer></methodparam>
  </methodsynopsis>
  <para>
   <function>substr_compare</function> compares <parameter>main_str</parameter>
   from position <parameter>offset</parameter> with <parameter>str</parameter>
   up to <parameter>length</parameter> characters.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term><parameter>main_str</parameter></term>
     <listitem>
      <para>
       The main string being compared.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>str</parameter></term>
     <listitem>
      <para>
       The secondary string being compared.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>offset</parameter></term>
     <listitem>
      <para>
       The start position for the comparison. If negative, it starts counting
       from the end of the string.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>length</parameter></term>
     <listitem>
      <para>
       The length of the comparison. The default value is the largest of the
       length of the <parameter>str</parameter> compared to the length of
       <parameter>main_str</parameter> minus the
       <parameter>offset</parameter>.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>case_insensitivity</parameter></term>
     <listitem>
      <para>
       If <parameter>case_insensitivity</parameter> is &true;, comparison is
       case insensitive.
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   Returns &lt; 0 if <parameter>main_str</parameter> from position
   <parameter>offset</parameter> is less than <parameter>str</parameter>, &gt;
   0 if it is greater than <parameter>str</parameter>, and 0 if they are equal.
   If <parameter>offset</parameter> is equal to (prior to PHP 7.2.18, 7.3.5) or
   greater than the length of <parameter>main_str</parameter>, or the
   <parameter>length</parameter> is set and is less than 0,
   (or, prior to PHP 5.5.11, less than 1)
   <function>substr_compare</function> prints a warning and returns
   &false;.
  </para>
 </refsect1>

 <refsect1 role="changelog">
  &reftitle.changelog;
  <para>
   <informaltable>
    <tgroup cols="2">
     <thead>
      <row>
       <entry>&Version;</entry>
       <entry>&Description;</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry>7.2.18, 7.3.5</entry>
       <entry>
        <parameter>offset</parameter> may now be equal to the length of <parameter>main_str</parameter>.
       </entry>
      </row>
      <row>
       <entry>5.5.11</entry>
       <entry>
        <parameter>length</parameter> may now be <literal>0</literal>.
       </entry>
      </row>
      <row>
       <entry>5.1.0</entry>
       <entry>
        Added the possibility to use a negative <parameter>offset</parameter>.
       </entry>
      </row>
     </tbody>
    </tgroup>
   </informaltable>
  </para>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <para>
   <example>
    <title>A <function>substr_compare</function> example</title>
    <programlisting role="php">
<![CDATA[
<?php
echo substr_compare("abcde", "bc", 1, 2); // 0
echo substr_compare("abcde", "de", -2, 2); // 0
echo substr_compare("abcde", "bcg", 1, 2); // 0
echo substr_compare("abcde", "BC", 1, 2, true); // 0
echo substr_compare("abcde", "bc", 1, 3); // 1
echo substr_compare("abcde", "cd", 1, 2); // -1
echo substr_compare("abcde", "abc", 5, 1); // warning
?>
]]>
    </programlisting>
   </example>
  </para>
 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><function>strncmp</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
-->