Implement starts_with() function in PHP
This article demonstrates how to implement the starts_with() function in PHP. The starts_with() function determines whether the specified string is a prefix of the input string, and returns boolean true or false as appropriate.
1. Using strpos() function
The strpos() function returns the index of the first occurrence of a substring in a string. If the substring is found at the beginning of the string, the strpos() function returns index 0.
Therefore, you can check whether a string starts with the specified string by comparing the return value of strpos(). This comparison should be done using the !== operator, since strpos() returns false if the substring cannot be located within the string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php function starts_with($input, $prefix) { return strpos($input, $prefix) === 0; } $input = "Hello, World"; $prefix = "Hello"; if (starts_with($input, $prefix)) { echo "String '$input' starts with prefix '$prefix'"; } ?> |
You may also use the strrpos() function to implement the starts_with() function. It returns the index of the last occurrence of a substring within a string, starting from the specified offset position. If the offset is negative, it searches for the last occurrence of the substring before the last offset byte.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php function starts_with($input, $prefix) { return strrpos($input, $prefix, -strlen($input)) === 0; } $input = "Hello, World"; $prefix = "Hello"; if (starts_with($input, $prefix)) { echo "String '$input' starts with prefix '$prefix'"; } ?> |
2. Using substr() function
The idea here is to find the length of the prefix string, and extract that many characters from the start of the input string using the substr() function. Then, you can simply compare the value of the returned string with the prefix string using the === operator. If the comparison result is true, then the input string begins with the characters of the specified prefix, otherwise it does not.
The following example provides a simple illustration. Note that the strpos() function is the faster option, and uses less memory than the substr() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php function starts_with($input, $prefix) { return substr($input, 0, strlen($prefix)) === $prefix; } $input = "Hello, World"; $prefix = "Hello"; if (starts_with($input, $prefix)) { echo "String '$input' starts with prefix '$prefix'"; } ?> |
3. Using substr_compare() function
Another option is to use the substr_compare() function to implement the starts_with() function. It takes four parameters: haystack, needle, offset, and length, in that order, and compares length characters of the haystack starting from position offset with needle, and returns 0 if and only if both strings are equal.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php function starts_with($input, $prefix) { return substr_compare($input, $prefix, 0, strlen($prefix)) === 0; } $input = "Hello, World"; $prefix = "Hello"; if (starts_with($input, $prefix)) { echo "String '$input' starts with prefix '$prefix'"; } ?> |
4. Using strncmp() function
A more straight-forward option is to use the strncmp() function. It performs a binary-safe comparison of the first n characters between two strings and returns 0 when both strings are equal.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php function starts_with($input, $prefix) { return strncmp($input, $prefix, strlen($prefix)) === 0; } $input = "Hello, World"; $prefix = "Hello"; if (starts_with($input, $prefix)) { echo "String '$input' starts with prefix '$prefix'"; } ?> |
5. Using preg_match() function
Finally, you may use the preg_match() function to check if a string starts with the specified prefix using a regular expression. It returns 1 if the regular expression matches the given string, and 0 in the case of no match.
Here’s an example of how you could achieve that. Here, ^ matches with the start of a string. Note that the === operator is used to compare the return value of this function. This is because the preg_match() function returns false on failure.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php function starts_with($input, $prefix) { return preg_match("/^{$prefix}/", $input) === 1; } $input = "Hello, World"; $prefix = "Hello"; if (starts_with($input, $prefix)) { echo "String '$input' starts with prefix '$prefix'"; } ?> |
Note that comparisons done by all the above methods are case-sensitive. To make the comparison case-insensitive, you can use the stripos(), strripos(), or strncasecmp() functions. Alternatively, you can pass the fifth parameter to substr_compare() as true or add i at the end of the regular expression, like "/^{$prefix}/i".
That’s all about implementing the starts_with() function in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)