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.

Download  Run Code

 
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.

Download  Run Code

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.

Download  Run Code

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.

Download  Run Code

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.

Download  Run Code

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.

Download  Run Code

 
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.