This plugin is for people like me for whom working with text without the perl regex engine is like not having a thumb.
It is also my first ever EE plugin. It doesn’t do much, and I’m not sure whether its functionality isn’t fully covered elsewhere.
It provides an EE interface to preg_match() and preg_replace(). That’s all!
PARAMETERS:
1) regex - a Perl regular expression.
2) string - a string
3) string_replace - Optional. replacement string. If not supplied, plugin defaults to using preg_match()
4) limit - optional. Default is -1 which replaces everything. Sets maximum number of replacements.
5) max_parens - optional: creates additional conditional variables regex_n up to n=max_parens
6) debug - optional: a value of ‘y’ allows DEBUGGING variables
VARIABLES:
1) regex_1 regex_2 … regex_n : matching parenthesis ( only if string replace wasn’t provided ) 2) result_string : the result of the operation ( only if string_replace was provided ) 3) matches : boolean 4) not_matches: boolean 5) match_count: number of matches or replacements
DEBUG VARIABLES: 6) string –> the parameter string 7) pattern –> the parameter regex
EXAMPLE OF USAGE:
{exp:perl_regex regex="@(.+://)?(\\w+)\\.(\\w+)\\.(net|com|org|info|\\w{2})(/([^/]+/)+)([^/]+.\\w)$@"
string="http://www.yesterday.br/today/the_day/tomorrow.html"
debug="y" }
{regex_1} is the service.
{regex_2} is the subdomain.
{regex_3} is the domain name.
{regex_4} is the top domain.
{regex_5} is full path
{regex_6} is the last dir in the path
{regex_7} is file name
matched # {match_count} times
{string}
{pattern}
{/exp:perl_regex}
{exp:perl_regex string="see a little goblin/see his little feet"
regex="@goblin@"
replace_string="elf"
debug="y" }
{if matches}
YES! WE GOT A MATCH!
<h3>{replace_result}</h3>
{/if}
{if not_matches}
NO! CANT FIND ANYTHING
{/if}
matched # {match_count} times
{string}
{pattern}
{replace_string}
{/exp:perl_regex}
======================
I needed it because I wanted to display a custom weblog field inside a category_archive (inside the entries var tag). To get there I needed the entry_id, but category_archive only provides the id inside the path variable:
So here’s the code I used:
{exp:weblog:category_archive weblog="blah" style="linear" show_empty="no" }
{categories}
<h2>{category_name}</h2>
{/categories}
{entry_titles}
{exp:perl_regex regex="%(\\d+)/$%" string="{entry_id_path=/}" }
<h3> <a href="http://{embed=%22parts/__id2link">{title}</a></h3>
{/exp:perl_regex}
{/entry_titles}
{/exp:weblog:category_archive}
NOTES:
Bugs are guaranteed!!!
Thanks for Laisvunas whose plugin was purloined (but unharmed) in the making of this plugin.
because the regex can have the modifier ‘e’, which allows for code execution, using this plugin potentially opens a security hole. When I have time and if it matters to people, I’ll block the use of the e modifier. until then, user’s discretion upply.
actually, user’s discretion always applies 😉
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.