Current Location: Home> Latest Articles> How to Use stristr and strlen to Get the Length of a Matching String?

How to Use stristr and strlen to Get the Length of a Matching String?

gitbox 2025-09-22

In PHP, stristr and strlen are commonly used string handling functions. They are used to find the matching part of a string and to calculate the length of a string, respectively. By combining these two functions properly, we can easily obtain the length of a matching substring. This article will explain in detail how to use these two functions to get the length of a matched string.

1. The purpose of the stristr function

The stristr function searches for the first occurrence of one string inside another and returns the portion of the string from that position to the end. This function is case-sensitive and the returned result includes the matched substring from the original string. The basic syntax is as follows:

<span><span><span class="hljs-title function_ invoke__">stristr</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$haystack</span></span><span>, </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$needle</span></span><span>, </span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-variable">$before_needle</span></span><span> = </span><span><span class="hljs-literal">false</span></span><span>): </span><span><span class="hljs-keyword">string</span></span><span>|</span><span><span class="hljs-literal">false</span></span><span>  
</span></span>
  • $haystack: The target string to search in.

  • $needle: The substring to search for.

  • $before_needle: If set to true, it will return the string from the beginning up to (but not including) the matched substring.

stristr returns the matched substring, or false if nothing is found.

2. The purpose of the strlen function

The strlen function calculates the length of a string. It returns the number of characters in the string and ignores encoding type. The basic syntax is:

<span><span><span class="hljs-title function_ invoke__">strlen</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$string</span></span><span>): </span><span><span class="hljs-keyword">int</span></span><span>  
</span></span>
  • $string: The string whose length needs to be calculated.

3. Combining stristr and strlen to get the length of a matching string

When we need to find a substring and get the length of the matched part, we can combine stristr with strlen. The steps are as follows:

  1. Use stristr to find the first match of the substring in the target string.

  2. Use strlen to calculate the length of the matched part.

Example code

Suppose we have a string $haystack, and we want to find a substring $needle and calculate the length of its matched part. The code would be:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>  
<p></span>$haystack = "PHP is a popular general-purpose scripting language.";<br>
$needle = "popular";</p>
<p>// Use stristr to find the matching substring<br>
$match = stristr($haystack, $needle);</p>
<p>// If a match is found<br>
if ($match !== false) {<br>
// Use strlen to calculate the length of the matched part<br>
$length = strlen($match);<br>
echo "The length of the matching string is: " . $length;<br>
} else {<br>
echo "No matching substring found.";<br>
}</p>
<p>?><br>
</span>

4. Code explanation

  1. We first define two variables, $haystack and $needle, representing the target string and the substring to search for.

  2. We use stristr to find the first occurrence of $needle in $haystack, and save the matched part in $match. If found, stristr returns the portion from the match to the end of the string.

  3. If a match is found, we then use strlen to calculate its length.

  4. Finally, we output the length of the matching string.

5. Output result

Suppose our $haystack is "PHP is a popular general-purpose scripting language." and $needle is "popular". Running the above code will output:

<span><span><span class="hljs-section">The length of the matching string is: 8</span></span><span>  
</span></span>

This is because "popular" is the matched substring and its length is 8.

6. Notes

  • stristr returns the matched substring along with everything after it. So, if we only care about the length of the substring itself, we can directly use strlen.

  • If we want the search to stop before the matched substring, we can use the third parameter $before_needle of stristr. Setting it to true will return only the portion before the substring.

Conclusion

By combining stristr and strlen, we can easily find matching strings and calculate their lengths. This is very useful in many scenarios, especially in string processing and data extraction tasks. Hopefully, the examples in this article help you better understand how to use these two functions.