[wp-trac] [WordPress Trac] #58220: Replace usage of substr with str_starts_with and str_ends_with
WordPress Trac
noreply at wordpress.org
Thu Jun 22 14:56:01 UTC 2023
#58220: Replace usage of substr with str_starts_with and str_ends_with
------------------------------+-----------------------------
Reporter: Soean | Owner: SergeyBiryukov
Type: enhancement | Status: closed
Priority: normal | Milestone: 6.3
Component: General | Version:
Severity: normal | Resolution: fixed
Keywords: has-patch commit | Focuses: performance
------------------------------+-----------------------------
Changes (by SergeyBiryukov):
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"55990" 55990]:
{{{
#!CommitTicketReference repository="" revision="55990"
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.
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/58220#comment:18>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list