Checking if a string commences with a particular string sequence is a common task in programming. In this case, the objective is to determine if a string starts with "http." Here are two approaches:
For PHP 8 or later versions, the str_starts_with function provides a straightforward solution:
str_starts_with('http://www.google.com', 'http')
In PHP 7 or earlier versions, we can utilize the substr function to extract a portion of the string from the beginning:
substr($string_n, 0, 4) === "http"
To ensure that it does not match other protocols or similar sequences, consider extending the substring length:
substr($string_n, 0, 7) === "http://"
In general, the following pattern can be used:
substr($string, 0, strlen($query)) === $query
The above is the detailed content of How to Check if a String Starts with 'http”?. For more information, please follow other related articles on the PHP Chinese website!