Make WordPress Core


Ignore:
Timestamp:
06/22/2023 02:55:47 PM (16 months ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Replace usage of substr() with str_starts_with() and str_ends_with().

str_starts_with() and str_ends_with() were introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins or ends with the given substring (needle).

WordPress core includes a polyfill for these functions on PHP < 8.0 as of WordPress 5.9.

This commit uses str_starts_with() and str_ends_with() in core files where appropriate:

  • $needle === substr( $string, 0, $length ), where $length is the length of $needle, is replaced with str_starts_with( $haystack, $needle ).
  • $needle === substr( $string, $offset ), where $offset is negative and the absolute value of $offset is the length of $needle, is replaced with str_ends_with( $haystack, $needle ).

This aims to make the code more readable and consistent, as well as better aligned with modern development practices.

Follow-up to [52039], [52040], [52326], [55703], [55710], [55987], [55988].

Props Soean, spacedmonkey, Clorith, ocean90, azaozz, sabernhardt, SergeyBiryukov.
Fixes #58220.

File:
1 edited

Legend:

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

    r55988 r55990  
    243243        $first = $curl[0];
    244244        if ( '<' === $first ) {
    245             if ( '<!--' === substr( $curl, 0, 4 ) ) {
     245            if ( str_starts_with( $curl, '<!--' ) ) {
    246246                // This is an HTML comment delimiter.
    247247                continue;
     
    261261            // This is a shortcode delimiter.
    262262
    263             if ( '[[' !== substr( $curl, 0, 2 ) && ']]' !== substr( $curl, -2 ) ) {
     263            if ( ! str_starts_with( $curl, '[[' ) && ! str_ends_with( $curl, ']]' ) ) {
    264264                // Looks like a normal shortcode.
    265265                _wptexturize_pushpop_element( $curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes );
     
    26282628        $pre_attribute_ws  = isset( $regex[4] ) ? $regex[4] : '';
    26292629        $attributes        = trim( isset( $regex[5] ) ? $regex[5] : $regex[3] );
    2630         $has_self_closer   = '/' === substr( $attributes, -1 );
     2630        $has_self_closer   = str_ends_with( $attributes, '/' );
    26312631
    26322632        $newtext .= $tagqueue;
     
    36943694        $offset = 0;
    36953695    } else {
    3696         $sign    = ( '+' === substr( $timezone, 0, 1 ) ) ? 1 : -1;
     3696        $sign    = ( str_starts_with( $timezone, '+' ) ) ? 1 : -1;
    36973697        $hours   = (int) substr( $timezone, 1, 2 );
    36983698        $minutes = (int) substr( $timezone, 3, 4 ) / 60;
     
    52675267function wp_sprintf_l( $pattern, $args ) {
    52685268    // Not a match.
    5269     if ( '%l' !== substr( $pattern, 0, 2 ) ) {
     5269    if ( ! str_starts_with( $pattern, '%l' ) ) {
    52705270        return $pattern;
    52715271    }
Note: See TracChangeset for help on using the changeset viewer.