How to check what a string starts with
Short answer
str_starts_with() checks the beginning of a string:
<?php
var_dump(str_starts_with('https://example.com', 'https://'));Output:
bool(true)The end of a string
The companion str_ends_with() checks the end — for example, a file extension:
<?php
var_dump(str_ends_with('report.pdf', '.pdf'));Output:
bool(true)Ignoring case
Both functions are case-sensitive. Convert the string to lowercase before checking:
<?php
var_dump(str_starts_with(mb_strtolower('HTTPS://example.com'), 'https://'));Output:
bool(true)Functions used in this recipe
- str_starts_with — Checks whether a string starts with a given substring.
- str_contains — Checks whether a string contains a given substring.