Opened 13 years ago
Closed 9 years ago
#19968 closed enhancement (wontfix)
Use named regular expression groups to simplify shortcodes code
Reported by: | nbachiyski | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.3 |
Component: | Shortcodes | Keywords: | has-patch needs-refresh |
Focuses: | Cc: |
Description (last modified by )
Currently in get_shortcode_regex()
in 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:
- 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.
- 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]);
Attachments (1)
Change History (8)
#2
@
13 years ago
19968.diff adds named groups for one of the shortcodes functions that defines a regex, but not the one that parses the attributes.
I've not named the group labelled as 4. as that didn't seem to currently be in use anywhere. If it is, then I'd suggest a name of self-closing
.
All untested.
#6
@
9 years ago
- Keywords close added
According to http://www.regular-expressions.info/refext.html the proposed enhancement would work in PHP, but would be incompatible with Javascript parsing. Participants at the October 14 roadmap meeting recommended closing this ticket as "maybelater". Please add any final comments now.
Named groups defined for get_shortcode_regex(), used in do_shortcode_tags() and strip_shortcode_tag()