Make WordPress Core

Changeset 57457 for branches/5.0


Ignore:
Timestamp:
01/30/2024 06:14:54 PM (3 years ago)
Author:
jorbin
Message:

General: Backport polyfills for str_ends_with() and str_starts_with().

Merges [52040], [56016], and [56015] to 5.0 branch.

Props ocean90, SergeyBiryukov, desrosj, joemcgill, jorbin, mukesh27.

Location:
branches/5.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0

  • branches/5.0/src/wp-includes/compat.php

    r43221 r57457  
    538538        }
    539539}
     540
     541if ( ! function_exists( 'str_starts_with' ) ) {
     542        /**
     543         * Polyfill for `str_starts_with()` function added in PHP 8.0.
     544         *
     545         * Performs a case-sensitive check indicating if
     546         * the haystack begins with needle.
     547         *
     548         * @since 5.9.0
     549         *
     550         * @param string $haystack The string to search in.
     551         * @param string $needle   The substring to search for in the `$haystack`.
     552         * @return bool True if `$haystack` starts with `$needle`, otherwise false.
     553         */
     554        function str_starts_with( $haystack, $needle ) {
     555                if ( '' === $needle ) {
     556                        return true;
     557                }
     558
     559                return 0 === strpos( $haystack, $needle );
     560        }
     561}
     562
     563if ( ! function_exists( 'str_ends_with' ) ) {
     564        /**
     565         * Polyfill for `str_ends_with()` function added in PHP 8.0.
     566         *
     567         * Performs a case-sensitive check indicating if
     568         * the haystack ends with needle.
     569         *
     570         * @since 5.9.0
     571         *
     572         * @param string $haystack The string to search in.
     573         * @param string $needle   The substring to search for in the `$haystack`.
     574         * @return bool True if `$haystack` ends with `$needle`, otherwise false.
     575         */
     576        function str_ends_with( $haystack, $needle ) {
     577                if ( '' === $haystack ) {
     578                        return '' === $needle;
     579                }
     580
     581                $len = strlen( $needle );
     582
     583                return substr( $haystack, -$len, $len ) === $needle;
     584        }
     585}
Note: See TracChangeset for help on using the changeset viewer.