Plugin

Developer
Supported

EE 1
EE 2
Switchee

Back to this add-on's main page
View Other Add-ons From Mark Croxton

     

You must be logged in to post.

Regular expressions w/pipes bug fix

Bug Report

Dom Stubbs
Dom Stubbs

Hi Mark,

Switchee is fantastic, thanks for making it available. I ran into an issue earlier with a regular expression that included a pipe - Switchee insisted on exploding the regex in two as it thought I was passing in a normal either/or value. I’ve put together a fix for this that makes Switchee ‘regular expression aware’. I’ve not tested it too much yet but it’s working nicely for me.

Old code:

if (stristr($val['value']'|'))
{
 $val_array 
explode('|'$val['value']);

New code:

if (stristr($val['value']'|'))
{
 
// Obfuscate pipes within regexs to stop them exploding
 
$val['value'preg_replace_callback('/#[^#]+(\|)[^#]+#/'create_function(
         
'$matches',
         
'return str_replace("|", "REPIPE", $matches[0]);'
 
), $val['value']);

 
$val_array explode('|'$val['value']);
 
 
// Restore regex pipes
 
$val_array str_replace('REPIPE''|'$val_array);

Having built that I’ve now realised that Switchee doesn’t (I don’t think?) support mixed regex and string values anyway, so this code could be simplified. At least this option will work if you added that in the future.

Cheers,
Dom

Mark Croxton
# 1
Developer
Mark Croxton

Hi Dom

Switchee already supports pipes in regular expressions but they have to be encoded as & #124; (remove the space after the ampersand). For historic reasons (EE 1.x) brackets { } can be encoded too.

I like what you’re doing there though, it seems more elegant, and I will probably use it. Thanks for that.

Dom Stubbs
# 2
Dom Stubbs

No problem. I hadn’t spotted that you could encode pipes, but never mind. At least this makes for slightly more readable templates.