Home > Backend Development > PHP Tutorial > How to Check if a String Starts with 'http”?

How to Check if a String Starts with 'http”?

DDD
Release: 2024-10-29 11:58:02
Original
528 people have browsed it

How to Check if a String Starts with “http”?

Establishing the Beginning of a String with a Specified String Sequence

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:

PHP 8 and Newer: str_starts_with() Function

For PHP 8 or later versions, the str_starts_with function provides a straightforward solution:

str_starts_with('http://www.google.com', 'http')

PHP 7 and Earlier: substr() Function

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template