Skip to content

Instantly share code, notes, and snippets.

@joshuaadickerson
Created August 12, 2015 02:51
Show Gist options
  • Save joshuaadickerson/9bc382698b9ae65346ba to your computer and use it in GitHub Desktop.
Save joshuaadickerson/9bc382698b9ae65346ba to your computer and use it in GitHub Desktop.
<?php
function pc_next_permutation($p, $size)
{
// slide down the array looking for where we're smaller than the next guy
for ($i = $size - 1; $p[$i] >= $p[$i + 1]; --$i);
// if this doesn't occur, we've finished our permutations
// the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
if ($i == -1)
{
return false;
}
// slide down the array looking for a bigger number than what we found before
for ($j = $size; $p[$j] <= $p[$i]; --$j);
// swap them
$tmp = $p[$i];
$p[$i] = $p[$j];
$p[$j] = $tmp;
// now reverse the elements in between by swapping the ends
for (++$i, $j = $size; $i < $j; ++$i, --$j)
{
$tmp = $p[$i];
$p[$i] = $p[$j];
$p[$j] = $tmp;
}
return $p;
}
$set = array();
foreach ($possible['parameters'] as $p => $info)
{
$set[] = '(\s+' . $p . '=' . (empty($info['quoted']) ? '' : '"') . (isset($info['match']) ? $info['match'] : '(.+?)') . (empty($info['quoted']) ? '' : '"') . ')' . (empty($info['optional']) ? '' : '?');
}
$size = count($set) - 1;
$keys = range(0, $size);
$message_part = substr($message, $pos1 - 1);
do {
$match_string = '~^';
foreach ($keys as $key)
{
$match_string .= $set[$key];
}
$match_string .= '\]~i';
$match = preg_match($match_string, $message_part), $matches) != 0;
} while (!$match && ($keys = pc_next_permutation($keys, $size)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment