- PHP: Check if string starts with a specified string.
- Examples.
- str_starts_with
- Parameters
- Return Values
- Examples
- Notes
- See Also
- User Contributed Notes 2 notes
- str_starts_with
- Parameters
- Return Values
- Examples
- Notes
- See Also
- Check if a String Starts With a Specified String in PHP
- Use substr() Function to Check if a String Starts With a Specified String in PHP
- Use strpos() Function to Check if a String Starts With a Specified String in PHP
- Use strncmp() Function to Check if a String Starts With a Specified String in PHP
- Related Article — PHP String
- PHP str_starts_with
- Introduction to the PHP str_starts_with() function
- PHP str_starts_with() function examples
- 1) Using PHP str_starts_with() function with single character example
- 2) Using the PHP str_starts_with() function with multiple characters example
- 2) Case-sensitive example
- Summary
PHP: Check if string starts with a specified string.
This is a short PHP guide on how to check if a string starts with a given string. Below, I have created a simple PHP function that will do this on both a “case sensitive” and “case insensitive” basis.
Let’s take a look at the following function:
/** * Checks to see if a given string exists at the start of another string. * * @param $haystack The string to search in. * @param $needle The string we are looking for. * @param bool $caseSensitive Whether we want our search to be case sensitive or not. * @return bool */ function strStartsWith($haystack, $needle, $caseSensitive = true) < //Get the length of the needle. $length = strlen($needle); //Get the start of the haystack. $startOfHaystack = substr($haystack, 0, $length); //If we want our check to be case sensitive. if($caseSensitive)< //Strict comparison. if($startOfHaystack === $needle)< return true; >> else < //Case insensitive. //If the needle and the start of the haystack are the same. if(strcasecmp($startOfHaystack, $needle) == 0)< return true; >> //No matches. Return FALSE. return false; >
The function above provides a third optional parameter that allows you to specify whether or not you want the search to be case sensitive or not. By default, the function will be case sensitive.
- We get the length of the string that we are looking for.
- Using that length, we then extract the start of the $haystack using PHP’s substr function.
- Finally, we compare the strings to see if they match. How we compare the strings depends on whether $caseSensitive is TRUE or FALSE. If it is TRUE, we use PHP’s triple equals for a strict comparison. If it is FALSE, we use the strcasecmp function.
Examples.
In the examples below, we check to see if a string starts with the string “HELLO”. Note that in the second example, I set the third parameter to FALSE for a case insensitive comparison.
//Case sensitive. if(!strStartsWith('hello123', 'HELLO'))< echo 'HELLO WAS NOT found at the start of hello123 - Case sensitive', '
'; > //Case insensitive. if(strStartsWith('hello123', 'HELLO', false))< echo 'HELLO WAS found at the start of hello123 - Case insensitive', '
'; >
As you can see, it is pretty simple to use!
str_starts_with
Performs a case-sensitive check indicating if haystack begins with needle .
Parameters
The substring to search for in the haystack .
Return Values
Returns true if haystack begins with needle , false otherwise.
Examples
Example #1 Using the empty string »
The above example will output:
All strings start with the empty string
Example #2 Showing case-sensitivity
$string = ‘The lazy fox jumped over the fence’ ;
?php
if ( str_starts_with ( $string , ‘The’ )) echo «The string starts with ‘The’\n» ;
>
if ( str_starts_with ( $string , ‘the’ )) echo ‘The string starts with «the»‘ ;
> else echo ‘»the» was not found because the case does not match’ ;
>
The above example will output:
The string starts with 'The' "the" was not found because the case does not match
Notes
Note: This function is binary-safe.
See Also
- str_contains() — Determine if a string contains a given substring
- str_ends_with() — Checks if a string ends with a given substring
- stripos() — Find the position of the first occurrence of a case-insensitive substring in a string
- strrpos() — Find the position of the last occurrence of a substring in a string
- strripos() — Find the position of the last occurrence of a case-insensitive substring in a string
- strstr() — Find the first occurrence of a string
- strpbrk() — Search a string for any of a set of characters
- substr() — Return part of a string
- preg_match() — Perform a regular expression match
User Contributed Notes 2 notes
With credit to Paul Phillips for the original polyfill posted.
If you do not have PHP 8, you can use these functions to get the capability of the new string functions.
But! Remember to use a conditional check to make sure the function is not already defined.
// source: Laravel Framework
// https://github.com/laravel/framework/blob/8.x/src/Illuminate/Support/Str.php
if (! function_exists ( ‘str_starts_with’ )) function str_starts_with ( $haystack , $needle ) return (string) $needle !== » && strncmp ( $haystack , $needle , strlen ( $needle )) === 0 ;
>
>
if (! function_exists ( ‘str_ends_with’ )) function str_ends_with ( $haystack , $needle ) return $needle !== » && substr ( $haystack , — strlen ( $needle )) === (string) $needle ;
>
>
if (! function_exists ( ‘str_contains’ )) function str_contains ( $haystack , $needle ) return $needle !== » && mb_strpos ( $haystack , $needle ) !== false ;
>
>
?>
This keeps it from breaking in case you upgrade and forget that you added it. This is a good practice generally when using the global scope for your helper functions.
In PHP7 you may want to use:
if (!function_exists(‘str_starts_with’)) function str_starts_with($str, $start) return (@substr_compare($str, $start, 0, strlen($start))==0);
>
>
AFAIK that is binary safe and doesn’t need additional checks.
str_starts_with
Performs a case-sensitive check indicating if haystack begins with needle .
Parameters
The substring to search for in the haystack .
Return Values
Returns true if haystack begins with needle , false otherwise.
Examples
Example #1 Using the empty string »
The above example will output:
All strings start with the empty string
Example #2 Showing case-sensitivity
$string = ‘The lazy fox jumped over the fence’ ;
?php
if ( str_starts_with ( $string , ‘The’ )) echo «The string starts with ‘The’\n» ;
>
if ( str_starts_with ( $string , ‘the’ )) echo ‘The string starts with «the»‘ ;
> else echo ‘»the» was not found because the case does not match’ ;
>
The above example will output:
The string starts with 'The' "the" was not found because the case does not match
Notes
Note: This function is binary-safe.
See Also
- str_contains() — Determine if a string contains a given substring
- str_ends_with() — Checks if a string ends with a given substring
- stripos() — Find the position of the first occurrence of a case-insensitive substring in a string
- strrpos() — Find the position of the last occurrence of a substring in a string
- strripos() — Find the position of the last occurrence of a case-insensitive substring in a string
- strstr() — Find the first occurrence of a string
- strpbrk() — Search a string for any of a set of characters
- substr() — Return part of a string
- preg_match() — Perform a regular expression match
Check if a String Starts With a Specified String in PHP
- Use substr() Function to Check if a String Starts With a Specified String in PHP
- Use strpos() Function to Check if a String Starts With a Specified String in PHP
- Use strncmp() Function to Check if a String Starts With a Specified String in PHP
In this article, we will introduce methods to check if a string starts with a specified string in PHP.
Use substr() Function to Check if a String Starts With a Specified String in PHP
The built-in function substr() is used to access a substring. The string is passed as an input, and the substring that we want to access is returned. We can use this function to check if a string starts with a specific string. The correct syntax to use this function is as follows
substr($string, $startPosition, $lengthOfSubstring);
php $string = "Mr. Peter"; if(substr($string, 0, 3) === "Mr.") echo "The string starts with the desired substring."; >else echo "The string does not start with the desired substring."; ?>
In the above code, we want to check if our string starts with Mr. .
0 is the start index of the substring, or in other words, the substring starts from the first character of the given string.
3 means the length of the returned substring is 3.
If the start of the string is the same as Mr. then it will display The string starts with the desired substring. .
The string starts with the desired substring.
Use strpos() Function to Check if a String Starts With a Specified String in PHP
The function strpos() returns the position of the first occurrence of a substring in the given string . We could use it to check if a string starts with a specified string.
If the returned value is 0 , it means the given string starts with the specified substring. Otherwise, the string doesn’t start with the checked substring.
strpos() is a case sensitive function. The correct syntax to use this function is as follows.
strpos($string, $searchString, $startPosition);
php $string = "Mr. Peter"; if(strpos( $string, "Mr." ) === 0) echo "The string starts with the desired substring."; >else echo "The string does not start with the desired substring."; ?>
Here, we have checked if our string starts with Mr. by finding the first occurrence of Mr. .
The string starts with the desired substring.
Use strncmp() Function to Check if a String Starts With a Specified String in PHP
The built-in function strncmp() compares two given strings . This function is also case sensitive. The correct syntax to use this function is as follows.
strncmp($string1, $string2, $length);
It returns zero if both the strings are equal. This is a case sensitive function.
php $string = "Mr. Peter"; if(strncmp($string, "Mr.", 3) === 0) echo "The string starts with the desired substring."; >else echo "The string does not start with the desired substring."; ?>
Here, the two strings are compared. The length of the strings to be compared is three.
The string starts with the desired substring.
The case insensitive version of strncmp is strncasecmp . It compares the first n characters of the given two strings regardless of their cases.
php $string = "mr. Peter"; if(strncasecmp($string, "Mr.", 3) === 0) echo "The string starts with the desired substring."; >else echo "The string does not start with the desired substring."; ?>
The string starts with the desired substring.
Related Article — PHP String
PHP str_starts_with
Summary: in this tutorial, you’ll learn how to use the PHP str_starts_with() function to check if a string starts with a substring.
Introduction to the PHP str_starts_with() function
The str_starts_with() function performs a case-senstive search and checks if a string starts with a substring:
str_starts_with ( string $haystack , string $needle ) : bool
Code language: PHP (php)
The str_starts_with() function has two parameters:
- $haystack is the input string to check.
- $needle is the substring to search for in the input string.
The str_starts_with() function returns true if the $haystack starts with the $needle or false otherwise.
The str_starts_with() function has been available since PHP 8.0.0. If you use a lower version of PHP, you can polyfill the function like this:
if (!function_exists('str_starts_with')) < function str_starts_with($haystack, $needle) < return (string)$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0; > >
Code language: PHP (php)
PHP str_starts_with() function examples
Let’s take some examples of using the PHP str_starts_with() function.
1) Using PHP str_starts_with() function with single character example
The following example uses the PHP str_starts_with() function to check if the string ‘PHP tutorial’ starts with the letter ‘P’ :
$str = 'PHP tutorial'; $substr = 'P'; $result = str_starts_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result starting with $substr";
Code language: PHP (php)
The PHP tutorial is starting with P
Code language: PHP (php)
2) Using the PHP str_starts_with() function with multiple characters example
The following example uses the PHP str_starts_with() function to check if the string ‘PHP tutorial’ starts with the string ‘PHP’ :
$str = 'PHP tutorial'; $substr = 'PHP'; $result = str_starts_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result starting with $substr";
Code language: PHP (php)
The PHP tutorial is starting with PHP
Code language: PHP (php)
2) Case-sensitive example
It’s important to keep in mind that the str_starts_with() function performs a case-sensitive search. For example:
$str = 'PHP tutorial'; $substr = 'php'; $result = str_starts_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result starting with $substr";
Code language: PHP (php)
The PHP tutorial is not starting with php
Code language: PHP (php)
Summary
- Use the PHP str_starts_with() function to perform a case-sensitive search and check if a string starts with a substring.