Current Location: Home> Latest Articles> How to Use str_ends_with and str_starts_with Together in PHP to Check Both the Beginning and End of a String?

How to Use str_ends_with and str_starts_with Together in PHP to Check Both the Beginning and End of a String?

gitbox 2025-06-22

In PHP programming, string manipulation is a common task, especially when working with file paths, URLs, or user input. We frequently need to check the beginning and end of a string. PHP offers two very handy functions: str_starts_with and str_ends_with. These functions allow us to quickly determine whether a string begins or ends with a given substring. This article discusses how to combine both functions to efficiently verify a string's start and end.

1. Introduction to the str_starts_with Function

The str_starts_with function checks whether a string begins with a specified substring. Its basic syntax is as follows:

<span><span><span class="hljs-title function_ invoke__">str_starts_with</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>
  • $haystack: The target string to check.

  • $needle: The substring to compare against.

If $haystack starts with $needle, the function returns true; otherwise, it returns false.

Example:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$filename</span></span><span> = </span><span><span class="hljs-string">"example.txt"</span></span><span>;
<p></span>if (str_starts_with($filename, "ex")) {<br>
echo "Filename starts with 'ex'.\n";<br>
} else {<br>
echo "Filename does not start with 'ex'.\n";<br>
}<br>
?><br>
</span>

Output:

<span><span>Filename starts with </span><span><span class="hljs-string">'ex'</span></span><span>.
</span></span>

2. Introduction to the str_ends_with Function

The str_ends_with function checks whether a string ends with a specified substring. Its basic syntax is as follows:

<span><span><span class="hljs-title function_ invoke__">str_ends_with</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>
  • $haystack: The target string to check.

  • $needle: The substring to compare against.

If $haystack ends with $needle, the function returns true; otherwise, it returns false.

Example:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$filename</span></span><span> = </span><span><span class="hljs-string">"example.txt"</span></span><span>;
<p></span>if (str_ends_with($filename, ".txt")) {<br>
echo "Filename ends with '.txt'.\n";<br>
} else {<br>
echo "Filename does not end with '.txt'.\n";<br>
}<br>
?><br>
</span>

Output:

<span><span>Filename ends with </span><span><span class="hljs-string">'.txt'</span></span><span>.
</span></span>

3. Using str_starts_with and str_ends_with Together

In real-world development, we often need to check both the start and end of a string. For instance, we may want to ensure that a filename starts with a specific prefix and ends with a specific extension. To achieve this, we can combine str_starts_with and str_ends_with.

Example: Check if a filename starts with "img_" and ends with ".jpg"

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$filename</span></span><span> = </span><span><span class="hljs-string">"img_picture.jpg"</span></span><span>;
<p></span>if (str_starts_with($filename, "img_") && str_ends_with($filename, ".jpg")) {<br>
echo "Filename starts with 'img_' and ends with '.jpg'.\n";<br>
} else {<br>
echo "Filename does not meet the criteria.\n";<br>
}<br>
?><br>
</span>

Output:

<span><span>Filename starts with </span><span><span class="hljs-string">'img_'</span></span><span> and ends with </span><span><span class="hljs-string">'.jpg'</span></span><span>.
</span></span>

4. Code Optimization and Considerations

When performing multiple string checks, combining str_starts_with and str_ends_with can significantly enhance code readability and efficiency. However, there are a few things to keep in mind:

  • Performance: These functions operate at O(n) complexity. While fast, if you're handling very long strings or processing in bulk, consider batching or caching to reduce redundant computations.

  • Character Encoding: These functions work well with UTF-8 encoded strings. If you're using a different encoding, it's advisable to convert the string first.

5. Conclusion

PHP’s str_starts_with and str_ends_with are extremely convenient for checking string prefixes and suffixes. In many real-world scenarios—like validating filenames or URLs—combining both can make your code cleaner and more efficient. Mastering these functions will help you implement precise string checks quickly and effectively.