Php string ends with you

PHP 8.0: New str_starts_with and str_ends_with functions

PHP 8.0 comes with two new functions to help you easily assert if a given string is present at the beginning or ending of a haystack string. This goes nicely with str_contains() in PHP 8.0.

  • str_starts_with() : Check if a given haystack string starts with the given needle string
  • str_ends_with : Check if a given haystack string ends with the given needle string
str_starts_with(string $haystack, string $needle): bool; str_ends_with(string $haystack, string $needle): bool;

PHP 8 finally brings the total number of string-related functions in PHP to over 100, and the new functions can be easily mimicked with existing functions such as strpos , substr , strncmp , and substr_compare . However, these new functions were well-received due to possible engine-level optimizations and their frequent use-cases.

Case sensitivity

Both str_starts_with() and str_ends_with() functions are case-sensitive. There are no flags or other functions to make them case-insensitive. This is the same pattern with str_contains

Читайте также:  Css text align center heights

Multi-byte strings

Multi-byte ( mb_* ) variants for str_starts_with() and str_ends_with() are not currently planned.

Empty strings

Similar to str_contains , PHP now considers empty string ( «» ) to be present in everywhere in a string. To quote Nikita:

As of PHP 8, behavior of » in string search functions is well defined, and
we consider » to occur at every position in the string, including one past
the end. As such, both of these will (or at least should) return true. The
empty string is contained in every string.

The following calls will be always true:

str_starts_with('Foo', ''); // true str_starts_with('', ''); // true str_ends_with('Foo', ''); // true str_ends_with('', ''); // true

Polyfills

Here is a polyfill for PHP 7.0 and later. Be sure to wrap them with function_exists() calls where you use them. These functions pass the exact same tests in PHP core.

function str_starts_with(string $haystack, string $needle): bool < return \strncmp($haystack, $needle, \strlen($needle)) === 0; >function str_ends_with(string $haystack, string $needle): bool

Conflicts with current user-land implementations

Starts-with and ends-with functionality is often provided as helper functions in various frameworks. This includes Symfony String component, Laravel Str helper, and Yii StringHelper .

There are over 4,000 str_starts_with() matches on GitHub, for PHP, most of which appear to be already namespaced.

Case-insensitivity support Empty strings at every position
PHP 8.0
str_starts_with
str_ends_with
No Yes
Polyfill (above)
str_starts_with
str_ends_with
No Yes
Symfony String
::startsWith
::endsWith
Yes
With ::ignoreCase()
No
Laravel
Str::startsWith
Str::endsWith
No No
Yii
StringHelper::startsWith
StringHelper::endsWith
Yes (default)
With parameter
No

Backwards compatibility impact

Both str_starts_with and str_ends_with functions are new functions. Unless you already have a str_contains() function declared, there should be no BC impact.

PHP 8’s new behavior that it considers there is an empty string at every position of a string can be tricky. Note that Laravel helpers and Symfony String component, among many others return false when you search for an empty string needle ( «» ) at the start and end of strings, although PHP core returns true .

© 2018-2023 PHP.Watch, with ❤ from Ayesh • About PHP.Watch

Источник

PHP str_ends_with

Summary: in this tutorial, you’ll learn how to use the PHP str_ends_with() function to check if a string ends with a substring.

Introduction to the PHP str_ends_with() function

The str_ends_with() function performs a case-sensitive search and checks if a string ends with a substring.

Here’s the syntax of the str_ends_with() function:

str_ends_with ( string $haystack , string $needle ) : boolCode language: PHP (php)

The str_ends_with() function has two parameters:

  • $haystack is the input string to check.
  • $needle is the substring to search for in the input string.

The str_ends_with() function returns true if the $haystack ends with the $needle or false otherwise.

Note that every string ends with an empty string. Therefore, if the $needle is an empty ( » ), the str_ends_with() always returns true .

The str_ends_with() function has only been available since PHP 8.0.0. If you use PHP with a lower version, you can polyfill the function like this:

 if (!function_exists('str_ends_with')) < function str_ends_with($haystack, $needle) < return $needle !== '' ? substr($haystack, -strlen($needle)) === $needle : true; > >Code language: PHP (php)

PHP str_ends_with() function examples

Let’s take some examples of using the PHP str_ends_with() function.

1) Using PHP str_ends_with() function with an empty substring example

The following example returns true because every string ends with an empty string:

 echo str_ends_with('Hello there', '');Code language: PHP (php)

2) Using PHP str_ends_with() function with single character example

The following example uses the PHP str_ends_with() function to check if the string ‘PHP tutorial’ ends with the letter ‘l’ :

 $str = 'PHP tutorial'; $substr = 'l'; $result = str_ends_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result ending with $substr";Code language: PHP (php)
The PHP tutorial is ending with lCode language: PHP (php)

3) Using the PHP str_ends_with() function with multiple characters example

The following example uses the PHP str_ends_with() function to check if the string ‘PHP tutorial’ ends with the string ‘tutorial’ :

 $str = 'PHP tutorial'; $substr = 'tutorial'; $result = str_ends_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result ending with $substr";Code language: PHP (php)
The PHP tutorial is ending with tutorialCode language: PHP (php)

4) Case-sensitive example

It’s important to notice that the str_ends_with() function carries a case-sensitive search. For example:

 $str = 'PHP tutorial'; $substr = 'Tutorial'; $result = str_ends_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result starting with $substr";Code language: PHP (php)
The PHP tutorial is not ending with TutorialCode language: PHP (php)

Summary

  • Use the PHP str_ends_with() function to perform a case-sensitive search and check if a string ends with a substring.

Источник

str_ends_with

Performs a case-sensitive check indicating if haystack ends with needle .

Parameters

The substring to search for in the haystack .

Return Values

Returns true if haystack ends with needle , false otherwise.

Examples

Example #1 Using the empty string »

The above example will output:

All strings end with the empty string

Example #2 Showing case-sensitivity

$string = ‘The lazy fox jumped over the fence’ ;

if ( str_ends_with ( $string , ‘fence’ )) echo «The string ends with ‘fence’\n» ;
>

if ( str_ends_with ( $string , ‘Fence’ )) echo ‘The string ends with «Fence»‘ ;
> else echo ‘»Fence» was not found because the case does not match’ ;
>

The above example will output:

The string ends with 'fence' "Fence" was not found because the case does not match

Notes

Note: This function is binary-safe.

See Also

  • str_contains() — Determine if a string contains a given substring
  • str_starts_with() — Checks if a string starts with a given substring
  • stripos() — Find the position of the first occurrence of a case-insensitive substring in a string
  • strrpos() — Find the position of the last occurrence of a substring in a string
  • strripos() — Find the position of the last occurrence of a case-insensitive substring in a string
  • strstr() — Find the first occurrence of a string
  • strpbrk() — Search a string for any of a set of characters
  • substr() — Return part of a string
  • preg_match() — Perform a regular expression match

User Contributed Notes 3 notes

this is the fastest php7-implementation i can think of, it should be faster than javalc6 and Reinder’s implementations, as this one doesn’t create new strings (but theirs does)

if (! function_exists ( ‘str_ends_with’ )) function str_ends_with ( string $haystack , string $needle ): bool
$needle_len = strlen ( $needle );
return ( $needle_len === 0 || 0 === substr_compare ( $haystack , $needle , — $needle_len ));
>
>
?>

In PHP7 you may want to use:

if (!function_exists(‘str_ends_with’)) function str_ends_with($str, $end) return (@substr_compare($str, $end, -strlen($end))==0);
>
>

AFAIK that is binary safe and doesn’t need additional checks.

In case you are using an older version of PHP, you can define and use the following function:

function endsWith($haystack, $needle) $length = strlen($needle);
return $length > 0 ? substr($haystack, -$length) === $needle : true;
>

Источник

How to check if a PHP string begins or ends with a specific string

Posted on Aug 01, 2022

  • The str_starts_with() function allows you to check if a string starts with a specific string
  • The str_ends_with() function allows you to check if a string ends with a specific string

The functions will return a boolean value true or false depending on the result.

Here’s how to check if the string Hello World! starts with he :

The functions are case-sensitive, so they will return false when the case doesn’t match between the $string and $substring .

Here’s an example of checking the end of the string with str_ends_with() :

Note that the two functions above won’t be available when you’re using PHP below version 8.0.

You need to create your own custom functions to do the same.

Creating PHP functions to check for a string start and end substrings

In PHP version 7, you will have Call to undefined function error when calling str_starts_with() and str_ends_with() functions.

You need to create these functions manually with the help of strlen() and substr() functions:

    Here’s the test result of the str_starts_with() function above:

For the str_ends_with() function, you need to change the substr() parameter.

See the following code snippet:

  The str_ends_with() function produces the following result:

With the functions above, you can check if a string starts with or ends with a certain substring in PHP 7.

Take your skills to the next level ⚡️

I’m sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I’ll send new stuff straight into your inbox!

About

Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.

Type the keyword below and hit enter

Tags

Click to see all tutorials tagged with:

Источник

Оцените статью