Home > Backend Development > PHP Tutorial > Function in PHP8: str_ends_with() - determine whether the string ends with the specified character

Function in PHP8: str_ends_with() - determine whether the string ends with the specified character

WBOY
Release: 2023-05-17 09:24:01
Original
2057 people have browsed it

With the release of the PHP7 version, the performance of the PHP language has been rapidly improved, and the latest version of PHP8 has added many new features and enhancements. One of the new features is the str_ends_with() function, which enables character judgment Whether a string ends with a specified character becomes simpler and more intuitive.

Definition of str_ends_with() function

str_ends_with() function is a new string processing function in PHP8. Its function is to determine whether a string ends with a specified character or substring. end. Specifically, it is defined as follows:

bool str_ends_with(string $haystack, string $needle): bool
Copy after login

Among them, $haystack represents the string to be checked, and $needle represents the character or substring to be found. Returns true if the end of the $haystack string is the same as $needle; otherwise, returns false.

Examples of using the str_ends_with() function

Now, let’s look at some examples of using the str_ends_with() function. First, suppose we have a string $url, which represents a URL:

$url = "https://www.example.com";
Copy after login

Now, we want to determine whether this URL ends with .com. We can use the str_ends_with() function to achieve this:

if (str_ends_with($url, ".com")) {
    echo "这是一个.com网址";
} else {
    echo "这不是一个.com网址";
}
Copy after login

Run the above code, the output should be like this:

这是一个.com网址
Copy after login

In addition, the str_ends_with() function also supports determining whether a string ends with any one of multiple characters or substrings. For example:

if (str_ends_with($url, ["com", "net", "org"])) {
    echo "这是一个常见的顶级域名网址";
} else {
    echo "这不是一个常见的顶级域名网址";
}
Copy after login

Run the above code, the output should be like this:

这是一个常见的顶级域名网址
Copy after login

The str_ends_with() function can also be used to determine whether a string ends with an empty string. For example:

if (str_ends_with($url, "")) {
    echo "这是一个空字符串";
} else {
    echo "这不是一个空字符串";
}
Copy after login

Run the above code, the output should be like this:

这不是一个空字符串
Copy after login

Why the str_ends_with() function is very practical

In actual development, we often need to String processing, such as interception, replacement, segmentation and other operations. Among them, it is a very common requirement to determine whether a string ends with a specified character or substring. In previous versions, we may need to use the substr() function and strlen() function to achieve this function, for example:

$url = "https://www.example.com";
$suffix = ".com";
if (substr($url, -strlen($suffix)) === $suffix) {
    echo "这是一个.com网址";
} else {
    echo "这不是一个.com网址";
}
Copy after login

Using the substr() function and strlen() function can also achieve the purpose , but the code is long and not very intuitive; and the substr() function is prone to boundary errors. In comparison, the str_ends_with() function is simpler and more practical, and can greatly improve development efficiency and code readability.

Of course, the str_ends_with() function is not omnipotent, and its usage scenarios are also limited. For example, if $needle itself is part of $haystack, the str_ends_with() function will still evaluate to true. Therefore, during use, judgment and adjustment need to be made based on the actual situation.

In short, with the release of PHP8, the str_ends_with() function brings more convenience to developers. I believe that in future development work, we will see it play an even greater role!

The above is the detailed content of Function in PHP8: str_ends_with() - determine whether the string ends with the specified character. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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