Make WordPress Core

Changeset 57442 for branches/4.4


Ignore:
Timestamp:
01/30/2024 05:27:44 PM (10 months ago)
Author:
jorbin
Message:

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

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

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

Location:
branches/4.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.4

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

    r34981 r57442  
    338338    require ABSPATH . WPINC . '/random_compat/random.php';
    339339}
     340
     341if ( ! function_exists( 'str_starts_with' ) ) {
     342    /**
     343     * Polyfill for `str_starts_with()` function added in PHP 8.0.
     344     *
     345     * Performs a case-sensitive check indicating if
     346     * the haystack begins with needle.
     347     *
     348     * @since 5.9.0
     349     *
     350     * @param string $haystack The string to search in.
     351     * @param string $needle   The substring to search for in the `$haystack`.
     352     * @return bool True if `$haystack` starts with `$needle`, otherwise false.
     353     */
     354    function str_starts_with( $haystack, $needle ) {
     355        if ( '' === $needle ) {
     356            return true;
     357        }
     358
     359        return 0 === strpos( $haystack, $needle );
     360    }
     361}
     362
     363if ( ! function_exists( 'str_ends_with' ) ) {
     364    /**
     365     * Polyfill for `str_ends_with()` function added in PHP 8.0.
     366     *
     367     * Performs a case-sensitive check indicating if
     368     * the haystack ends with needle.
     369     *
     370     * @since 5.9.0
     371     *
     372     * @param string $haystack The string to search in.
     373     * @param string $needle   The substring to search for in the `$haystack`.
     374     * @return bool True if `$haystack` ends with `$needle`, otherwise false.
     375     */
     376    function str_ends_with( $haystack, $needle ) {
     377        if ( '' === $haystack ) {
     378            return '' === $needle;
     379        }
     380
     381        $len = strlen( $needle );
     382
     383        return substr( $haystack, -$len, $len ) === $needle;
     384    }
     385}
Note: See TracChangeset for help on using the changeset viewer.