Php line start with

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

Читайте также:  Swap two strings in python

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

Источник

str_starts_with

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

Parameters

The substring to search for in the haystack .

Return Values

Returns true if haystack begins with needle , false otherwise.

Examples

Example #1 Using the empty string »

The above example will output:

All strings start with the empty string

Example #2 Showing case-sensitivity

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

if ( str_starts_with ( $string , ‘The’ )) echo «The string starts with ‘The’\n» ;
>

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

The above example will output:

The string starts with 'The' "the" 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_ends_with() — Checks if a string ends 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 2 notes

With credit to Paul Phillips for the original polyfill posted.

If you do not have PHP 8, you can use these functions to get the capability of the new string functions.

But! Remember to use a conditional check to make sure the function is not already defined.

// source: Laravel Framework
// https://github.com/laravel/framework/blob/8.x/src/Illuminate/Support/Str.php
if (! function_exists ( ‘str_starts_with’ )) function str_starts_with ( $haystack , $needle ) return (string) $needle !== » && strncmp ( $haystack , $needle , strlen ( $needle )) === 0 ;
>
>
if (! function_exists ( ‘str_ends_with’ )) function str_ends_with ( $haystack , $needle ) return $needle !== » && substr ( $haystack , — strlen ( $needle )) === (string) $needle ;
>
>
if (! function_exists ( ‘str_contains’ )) function str_contains ( $haystack , $needle ) return $needle !== » && mb_strpos ( $haystack , $needle ) !== false ;
>
>
?>

This keeps it from breaking in case you upgrade and forget that you added it. This is a good practice generally when using the global scope for your helper functions.

In PHP7 you may want to use:

if (!function_exists(‘str_starts_with’)) function str_starts_with($str, $start) return (@substr_compare($str, $start, 0, strlen($start))==0);
>
>

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

Источник

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:

Источник

PHP str_starts_with

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

Introduction to the PHP str_starts_with() function

The str_starts_with() function performs a case-senstive search and checks if a string starts with a substring:

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

The str_starts_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_starts_with() function returns true if the $haystack starts with the $needle or false otherwise.

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

 if (!function_exists('str_starts_with')) < function str_starts_with($haystack, $needle) < return (string)$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0; > >Code language: PHP (php)

PHP str_starts_with() function examples

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

1) Using PHP str_starts_with() function with single character example

The following example uses the PHP str_starts_with() function to check if the string ‘PHP tutorial’ starts with the letter ‘P’ :

 $str = 'PHP tutorial'; $substr = 'P'; $result = str_starts_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result starting with $substr";Code language: PHP (php)
The PHP tutorial is starting with PCode language: PHP (php)

2) Using the PHP str_starts_with() function with multiple characters example

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

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

2) Case-sensitive example

It’s important to keep in mind that the str_starts_with() function performs a case-sensitive search. For example:

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

Summary

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

Источник

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