<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Binary safe comparison of two strings from an offset, up to length characters</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.strtr.html">strtr</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.substr-count.html">substr_count</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.substr-compare" class="refentry"> <div class="refnamediv"> <h1 class="refname">substr_compare</h1> <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">substr_compare</span> — <span class="dc-title">Binary safe comparison of two strings from an offset, up to length characters</span></p> </div> <div class="refsect1 description" id="refsect1-function.substr-compare-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>substr_compare</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$main_str</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$case_insensitivity</code><span class="initializer"> = false</span></span> ]] )</div> <p class="para rdfs-comment"> <span class="function"><strong>substr_compare()</strong></span> compares <em><code class="parameter">main_str</code></em> from position <em><code class="parameter">offset</code></em> with <em><code class="parameter">str</code></em> up to <em><code class="parameter">length</code></em> characters. </p> </div> <div class="refsect1 parameters" id="refsect1-function.substr-compare-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">main_str</code></em></span> <dd> <p class="para"> The main string being compared. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">str</code></em></span> <dd> <p class="para"> The secondary string being compared. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">offset</code></em></span> <dd> <p class="para"> The start position for the comparison. If negative, it starts counting from the end of the string. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">length</code></em></span> <dd> <p class="para"> The length of the comparison. The default value is the largest of the length of the <em><code class="parameter">str</code></em> compared to the length of <em><code class="parameter">main_str</code></em> less the <em><code class="parameter">offset</code></em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">case_insensitivity</code></em></span> <dd> <p class="para"> If <em><code class="parameter">case_insensitivity</code></em> is <strong><code>TRUE</code></strong>, comparison is case insensitive. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.substr-compare-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns < 0 if <em><code class="parameter">main_str</code></em> from position <em><code class="parameter">offset</code></em> is less than <em><code class="parameter">str</code></em>, > 0 if it is greater than <em><code class="parameter">str</code></em>, and 0 if they are equal. If <em><code class="parameter">offset</code></em> is equal to or greater than the length of <em><code class="parameter">main_str</code></em> or <em><code class="parameter">length</code></em> is set and is less than 1, <span class="function"><strong>substr_compare()</strong></span> prints a warning and returns <strong><code>FALSE</code></strong>. </p> </div> <div class="refsect1 changelog" id="refsect1-function.substr-compare-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.1.0</td> <td> Added the possibility to use a negative <em><code class="parameter">offset</code></em>. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.substr-compare-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4913"> <p><strong>Example #1 A <span class="function"><strong>substr_compare()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">, </span><span style="color: #DD0000">"bc"</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">); </span><span style="color: #FF8000">// 0<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">, </span><span style="color: #DD0000">"de"</span><span style="color: #007700">, -</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">); </span><span style="color: #FF8000">// 0<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">, </span><span style="color: #DD0000">"bcg"</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">); </span><span style="color: #FF8000">// 0<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">, </span><span style="color: #DD0000">"BC"</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">); </span><span style="color: #FF8000">// 0<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">, </span><span style="color: #DD0000">"bc"</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">, </span><span style="color: #DD0000">"cd"</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">); </span><span style="color: #FF8000">// -1<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_compare</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">, </span><span style="color: #DD0000">"abc"</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// warning<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.substr-compare-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.strncmp.html" class="function" rel="rdfs-seeAlso">strncmp()</a> - Binary safe string comparison of the first n characters</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.strtr.html">strtr</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.substr-count.html">substr_count</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>