In PHP programming, we frequently handle string comparison operations, particularly when dealing with URLs, namespaces, or file paths that have specific prefixes. The strncmp() function is an efficient tool that compares whether the first N characters of two strings are identical. This article will explain in detail how to use strncmp() to determine if the prefixes of corresponding strings in two arrays match.
strncmp() is a built-in PHP function used to compare the prefix parts of two strings by character count. Its basic syntax is as follows:
<span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-title function_ invoke__">strncmp</span></span><span> ( </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$string1</span></span><span> , </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$string2</span></span><span> , </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$length</span></span><span> )
</span></span>
$string1 and $string2 are the two strings to compare;
$length is the number of characters to compare (starting from the beginning);
Return values:
0 means the prefixes are the same;
Less than 0 means the prefix of $string1 is lexicographically less than that of $string2;
Greater than 0 means the prefix of $string1 is lexicographically greater than that of $string2.
Suppose we have two arrays, each containing a group of strings. Now we want to check if the prefixes (of a specified length) of each pair of strings from the two arrays match.
<span><span><span class="hljs-meta"><?php</span></span><span>
<p></span>$array1 = ['apple123', 'banana456', 'cherry789'];<br>
$array2 = ['appleXYZ', 'bananaABC', 'cheese000'];<br>
$prefixLength = 5;</p>
<p>$result = [];</p>
<p>foreach ($array1 as $index => $str1) {<br>
$str2 = $array2[$index] ?? '';<br>
</span>if (strncmp($str1, $str2, $prefixLength) === 0) {<br>
$result[] = "Pair " . ($index + </span>1) . ": prefix matches";<br>
} </span>else {<br>
$result[] = "Pair " . ($index + </span>1) . ": prefix does not match";<br>
}<br>
}</p>
<p></span>foreach ($result as $line) {<br>
echo </span>$line . PHP_EOL;<br>
}<br>
</span></span>
<span><span>Pair 1: prefix matches
Pair 2: prefix matches
Pair 3: prefix does not match
</span></span>
Handling different array lengths: In the example above, ?? '' prevents out-of-bound errors. You may implement stricter checks on array length as needed.
Determining prefix length: Choosing an appropriate $length is crucial. If the comparison length exceeds the actual string length, no error occurs, but the result may be inaccurate.
Case-insensitive comparison: To ignore case during comparison, you can normalize the strings with strtolower() or strtoupper() first.
<span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">strncmp</span></span><span>(</span><span><span class="hljs-title function_ invoke__">strtolower</span></span><span>(</span><span><span class="hljs-variable">$str1</span></span><span>), </span><span><span class="hljs-title function_ invoke__">strtolower</span></span><span>(</span><span><span class="hljs-variable">$str2</span></span><span>), </span><span><span class="hljs-variable">$prefixLength</span></span><span>) === </span><span><span class="hljs-number">0</span></span><span>) {
</span><span><span class="hljs-comment">// Case-insensitive prefix match</span></span><span>
}
</span></span>
With the strncmp() function, PHP developers can easily compare whether the prefixes of two strings are consistent. This is especially useful in scenarios such as path matching, keyword extraction, and classification recognition. When used in conjunction with array operations, its practical value is even more apparent. Mastering this function will improve your efficiency and accuracy in string handling.