[wp-trac] [WordPress Trac] #44212: Add a new helper function for comma- or space-separated lists

WordPress Trac noreply at wordpress.org
Thu May 24 10:26:34 UTC 2018


#44212: Add a new helper function for comma- or space-separated lists
--------------------------+--------------------------------------
 Reporter:  keesiemeijer  |      Owner:  (none)
     Type:  enhancement   |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:
 Severity:  normal        |   Keywords:  has-patch has-unit-tests
  Focuses:                |
--------------------------+--------------------------------------
 The functions `wp_parse_id_list()` and `wp_parse_slug_list()` both use
 `preg_split()` to convert strings to an array. Strings with leading or
 trailing spaces will result in an added empty value {{{''}}} or {{{0}}}
 returned by these functions.

 Example

 {{{
 // leading space
 $ids = wp_parse_id_list( ' 1,2,3' );

 // result

 array(
     0 => 0
     1 => 1
     2 => 2
     3 => 3
 )
 }}}

 This is because the `PREG_SPLIT_NO_EMPTY` flag is not used
 with`preg_split()`. The same regex `'/[\s,]+/'` used by these functions is
 also used in other places in WordPress core (also without using the flag).
 That's why I propose a new helper function for comma- or space-separated
 lists (that uses the flag by default).

 That function could then be used by `wp_parse_id_list()` and
 `wp_parse_slug_list()` and all other instances where this regex is used.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/44212>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list