Make WordPress Core


Ignore:
Timestamp:
06/26/2023 10:15:04 AM (22 months ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Use str_contains() in a few more places.

str_contains() was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle).

WordPress core includes a polyfill for str_contains() on PHP < 8.0 as of WordPress 5.9.

This commit replaces false !== strpos( ... ) with str_contains() in core files, making the code more readable and consistent, as well as better aligned with modern development practices.

Follow-up to [55988], [56021].

See #58206.

File:
1 edited

Legend:

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

    r56021 r56031  
    10201020
    10211021    // Don't bother if there are no entities - saves a lot of processing.
    1022     if ( strpos( $text, '&' ) === false ) {
     1022    if ( ! str_contains( $text, '&' ) ) {
    10231023        return $text;
    10241024    }
     
    24752475    }
    24762476
    2477     if ( strpos( $content, '&' ) !== false ) {
     2477    if ( str_contains( $content, '&' ) ) {
    24782478        $content = preg_replace( '/&([^#])(?![a-z1-4]{1,8};)/i', '&#038;$1', $content );
    24792479    }
     
    25262526    );
    25272527
    2528     if ( strpos( $content, '&#1' ) !== false ) {
     2528    if ( str_contains( $content, '&#1' ) ) {
    25292529        $content = strtr( $content, $wp_htmltranswinuni );
    25302530    }
     
    55825582    $filtered = wp_check_invalid_utf8( $str );
    55835583
    5584     if ( strpos( $filtered, '<' ) !== false ) {
     5584    if ( str_contains( $filtered, '<' ) ) {
    55855585        $filtered = wp_pre_kses_less_than( $filtered );
    55865586        // This will strip extra whitespace for us.
     
    60676067
    60686068    foreach ( $headers as $header ) {
    6069         if ( strpos( $header, ':' ) === false ) {
     6069        if ( ! str_contains( $header, ':' ) ) {
    60706070            continue;
    60716071        }
     
    60796079
    60806080        if ( 'content-type' === strtolower( $name ) ) {
    6081             if ( strpos( $content, ';' ) !== false ) {
     6081            if ( str_contains( $content, ';' ) ) {
    60826082                list( $type, $charset ) = explode( ';', $content );
    60836083                $content_type           = trim( $type );
Note: See TracChangeset for help on using the changeset viewer.