Functions str_starts_with and str_ends_with in PHP: how to check the beginning and end of a string?
Hello everyone! I'm new to PHP and writing a small project. I have a task: I need to check whether a string starts with a certain prefix or ends with a certain suffix. For example, I'm processing file names and want to filter only those that start with "report_" or end with ".pdf".\n\nI've heard that PHP 8 introduced the str_starts_with() and str_ends_with() functions, but my project runs on PHP 7.4, and I can't upgrade the version right now. How can I implement such a check without these built-in functions?\n\nCurrently, I'm using substr() for comparison, but the code ends up being bulky and not very readable. Here's an example:\n\n```php\n$filename = "report_q1_2024.pdf";\n$prefix = "report_";\n$suffix = ".pdf";\n\nif (substr($filename, 0, strlen($prefix)) === $prefix) {\n echo "Start
No answers yet.
Log in to leave an answer
Blogs