I’m trying to check if a string starts with http. How can I do this check?
$string1 = 'google.com';
$string2 = 'http://www.google.com';
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
PHP 8 or newer:
Use the str_starts_with function:
PHP 7 or older:
Use the substr function to return a part of a string.
If you’re trying to make sure it’s not another protocol. I’d use
http://instead, since https would also match, and other things such as http-protocol.com.And in general: