PHP: Checking if a String Starts With Another String (str_starts_with)
- Return value
Always returns a Boolean value (TRUE if it starts with the substring, FALSE otherwise). - Case-sensitivity
Considers uppercase and lowercase differently. - Functionality
Checks if a haystack (the main string) starts with a needle (the substring).
Example
$string = "This is a string";
$prefix = "This";
if (str_starts_with($string, $prefix)) {
echo "The string '$string' starts with the prefix '$prefix'.";
} else {
echo "The string '$string' does not start with the prefix '$prefix'.";
}
This code will output:
The string 'This is a string' starts with the prefix 'This'.
- For versions prior to PHP 8.0, you can achieve similar functionality using
strpos
. However,strpos
returns the position of the first occurrence of the substring, and you'd need to check if the position is 0 (meaning it starts at the beginning). str_starts_with
is available in PHP 8.0 and later versions.
$string = "This is a string";
$prefix = "This";
if (strpos($string, $prefix) === 0) {
echo "The string '$string' starts with the prefix '$prefix'.";
} else {
echo "The string '$string' does not start with the prefix '$prefix'.";
}
Checking for Exact Match (Case-Sensitive)
$url = "https://www.example.com";
if (str_starts_with($url, "http://")) {
echo "The URL starts with 'http://' (not secure).";
} else if (str_starts_with($url, "https://")) {
echo "The URL starts with 'https://' (secure).";
} else {
echo "The URL does not start with a recognized protocol.";
}
Checking Multiple Substrings
$filename = "image1.jpg";
if (str_starts_with($filename, "image") || str_starts_with($filename, "photo")) {
echo "The filename starts with 'image' or 'photo'.";
} else {
echo "The filename does not start with 'image' or 'photo'.";
}
Checking with Empty Substring
$text = "Hello world!";
if (str_starts_with($text, "")) { // Empty string check
echo "The text starts with an empty string (always true).";
} else {
echo "The text does not start with an empty string (won't reach here)."; // This line won't execute
}
Case-Insensitive Check (using strtolower)
$username = "Username123";
if (str_starts_with(strtolower($username), "admin")) { // Convert both to lowercase
echo "The username starts with 'admin' (case-insensitive).";
} else {
echo "The username does not start with 'admin' (case-insensitive).";
}
- For older versions, consider using
strpos
with additional checks. str_starts_with
is available in PHP 8.0 and later.
Using strpos
The strpos
function finds the position of the first occurrence of a substring within a string. You can leverage this to check if the position is 0 (the beginning).
$string = "This is a string";
$prefix = "This";
if (strpos($string, $prefix) === 0) {
echo "The string '$string' starts with the prefix '$prefix'.";
} else {
echo "The string '$string' does not start with the prefix '$prefix'.";
}
Using Substring Extraction
This approach extracts a specific length of characters from the beginning of the string and compares it with the desired prefix.
$string = "This is a string";
$prefix = "This";
$extracted = substr($string, 0, strlen($prefix));
if ($extracted === $prefix) {
echo "The string '$string' starts with the prefix '$prefix'.";
} else {
echo "The string '$string' does not start with the prefix '$prefix'.";
}
Using Regular Expressions (more complex)
Regular expressions offer powerful pattern matching capabilities. However, they might be less readable for simpler tasks like checking string prefixes.
$string = "This is a string";
$prefix = "This";
$pattern = "^" . $prefix; // "^" matches the beginning of the string
if (preg_match($pattern, $string)) {
echo "The string '$string' starts with the prefix '$prefix'.";
} else {
echo "The string '$string' does not start with the prefix '$prefix'.";
}
Choosing the Best Alternative
- If the string manipulation needs involve more complex checks, consider regular expressions.
- For readability and simplicity,
strpos
is often the preferred choice.
- Explore the
substr_compare
function in PHP for more granular control over substring comparisons. - Remember to handle case-sensitivity if needed. You can use
strtolower
orstrtoupper
on both the string and the prefix for case-insensitive comparisons.