#602 function glob_to_pcre is awful

open
Reini Urban
None
5
2012-10-11
2008-04-09
Anonymous
No

glob_to_pcre generated a warning, I had a look in it ...

just have a deep look at glob_to_pcre, the complete mechanism has to revised ;-)

my solution is:

function glob_to_pcre ($glob) {

// preg_replace cannot handle "\\\\\\2" so convert \\ to \xff
$glob = strtr($glob, "\\", "\xff");

// first convert some unescaped expressions to pcre style: . => \.
$special = ".^$";
$re = preg_replace('/([^\xff])?(['.preg_quote($special).'])/',
                   "\\1\xff\\2", $glob);

// * => .*, ? => .
$re = preg_replace('/([^\xff])?\*/', '$1.*', $re);
$re = preg_replace('/([^\xff])?\?/', '$1.', $re);

// .*? handled above, now escape the rest
$re = preg_quote($re,'/');

$ret =  strtr($re, "\xff", "\\");

return $ret;

}

Discussion

  • Reini Urban
    Reini Urban
    2008-05-06

    Logged In: YES
    user_id=13755
    Originator: NO

    You are right. glob_to_pcre is awful and on some email notification pages you get stupid warnings.

    I've made a testscript.
    Does this pass without warnings?

    foreach (array(
    "test",
    "test",
    "test
    .mpg",
    "Calendar/test.mpg",
    "Calendar/
    ",
    "test.mpg",
    "test*",
    "test.
    ",
    "test.",
    "test$",
    "
    test",
    "*test.mpg",
    "te()st",
    "tes(t]",
    "tes<t>",
    ) as $s) {
    echo '"', $s, '" => "', glob_to_pcre($s), "\"\n";
    }

    ?>