[wp-trac] [WordPress Trac] #19968: Use named regular expression groups to simplify shortcodes code

WordPress Trac wp-trac at lists.automattic.com
Sat Feb 4 18:03:28 UTC 2012


#19968: Use named regular expression groups to simplify shortcodes code
-------------------------+-----------------------------
 Reporter:  nbachiyski   |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Shortcodes   |    Version:  3.3.1
 Severity:  minor        |   Keywords:  needs-patch
-------------------------+-----------------------------
 Currently in {{{get_shortcode_regex()}}} in [source:trunk/wp-
 includes/shortcodes.php] you are welcomed by:
 {{{
 // WARNING! Do not change this regex without changing do_shortcode_tag()
 and strip_shortcode_tag()
 }}}

 because these functions use the regular expression match from the
 shortcode parser.

 Instead, we could use named regex groups: {{{(?P<year>\d\d\d\d)}}} or
 {{{(?'year')\d\d\d\d}}} which appear indexed by name in the matches array:
 {{{$matches['year']}}} instead of {{{matches[1]}}}.

 Benefits:

 0. Future-proof code. Won't rely on indices, but on names, which we can
 control. Now, if we want to add a group we will have to offset all the
 indices, scattered all over the place.

 1. Less ugly and unreadable code like:

 {{{
 if (!empty($m[1]))
     $atts[strtolower($m[1])] = stripcslashes($m[2]);
 elseif (!empty($m[3]))
     $atts[strtolower($m[3])] = stripcslashes($m[4]);
 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/19968>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list