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

WordPress Trac noreply at wordpress.org
Wed Aug 21 19:03:14 UTC 2013


#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                    |  Resolution:
 Keywords:  has-patch needs-refresh  |
-------------------------------------+------------------------------
Description changed by SergeyBiryukov:

Old description:

> 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]);
> }}}

New description:

 Currently in {{{get_shortcode_regex()}}} in [source:trunk/src/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#comment:4>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list