[wp-trac] [WordPress Trac] #19347: Ugly alert: false !== strpos( $which_one_was_needle, $this_one )
WordPress Trac
wp-trac at lists.automattic.com
Wed Nov 23 22:08:03 UTC 2011
#19347: Ugly alert: false !== strpos( $which_one_was_needle, $this_one )
-------------------------+-----------------------------
Reporter: nbachiyski | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version:
Severity: normal | Keywords: has-patch
-------------------------+-----------------------------
In WordPress {{{strpos()}}} is used hundreds of times. Very large
percentage of those calls are trying to accomplish one of these 3 goals:
* Check if a string starts with another string
* Check if a string contains another string
* Check if a string ends with another string
The code for doing these 3 things is not very readable, ugly, and error-
prone:
{{{
0 === strpos( $one_string, $other_string )
false !== strpos( $one_string, $other_string )
strpos( $one_string, $other_string ) == ( strlen( $one_string ) - strlen(
$other_string )
}}}
I propose we introduce functions to hide the ugliness and the complexity
behind these three common operations:
* {{{wp_startswith( $haystack, $needle )}}}
* {{{wp_in( $needle, $haystack )}}}
* {{{wp_endswith( $haystack, $needle )}}}
The logic of the arguments order is consistent and easy to remember:
''first argument'' '''verb''' ''second argument''.
* everything starts with a beginning translates to {{{wp_startswith(
"everything", "a beginning" )}}}
* goat is in distress translates to {{{wp_in( "goat", "distress" )}}}
* movie ends with happy end translates to {{{wp_endswith( "movie", "happy
end" )}}}
These three functions are happily used in GlotPress and WordPress.com.
The implementations are attached.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/19347>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list