Home >Backend Development >PHP Tutorial >Function in PHP8: str_ends_with() - determine whether the string ends with the specified character
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
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";
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网址"; }
Run the above code, the output should be like this:
这是一个.com网址
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 "这不是一个常见的顶级域名网址"; }
Run the above code, the output should be like this:
这是一个常见的顶级域名网址
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 "这不是一个空字符串"; }
Run the above code, the output should be like this:
这不是一个空字符串
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网址"; }
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!