Make WordPress Core


Ignore:
Timestamp:
11/08/2021 02:02:54 PM (3 years ago)
Author:
hellofromTonya
Message:

General: Introduce polyfill for str_contains() added in PHP 8.0.

PHP 8.0 introduced a new function: str_contains(). It performs a case-sensitive check indicating if given substring (needle) is contained in the string to search in (haystack).

This polyfill makes this function available for use in Core.

Ref:

Props ayeshrajans, costdev, desrosj, hellofromTonya, knutsp, pbearne.
Fixes #49652.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/compat.php

    r52038 r52039  
    418418}
    419419
     420if ( ! function_exists( 'str_contains' ) ) {
     421    /**
     422     * Polyfill for `str_contains()` function added in PHP 8.0.
     423     *
     424     * Performs a case-sensitive check indicating if needle is
     425     * contained in haystack.
     426     *
     427     * @since 5.9.0
     428     *
     429     * @param string $haystack The string to search in.
     430     * @param string $needle   The substring to search for in the haystack.
     431     * @return bool True if `$needle` is in `$haystack`, otherwise false.
     432     */
     433    function str_contains( $haystack, $needle ) {
     434        return ( '' === $needle || false !== strpos( $haystack, $needle ) );
     435    }
     436}
     437
    420438// IMAGETYPE_WEBP constant is only defined in PHP 7.1 or later.
    421439if ( ! defined( 'IMAGETYPE_WEBP' ) ) {
Note: See TracChangeset for help on using the changeset viewer.