Page 1 of 1
glob() to preg_match()
Posted: Tue Oct 31, 2006 3:33 pm
by John Cartwright
I'm looking for an alternative to using glob() solely, so I've decided to make a shift towards using glob() to gather all the filenames and preg_match() them individually. It's more costly performance wise but I don't have another option here.
So lets move onto the problem.
Using glob() I could simply do something like (lets ignore wildcards for now)
Code: Select all
$pieces = array('inde', 'x.php');
$files = glob($dir.'/*{'. implode(',', $pieces) .'}*');
which results in:
I thought I could do something like
Code: Select all
preg_match('/('. implode('|', $pieces) .')/', $string)
which results in:
But this doesn't seem to work.
Any ideas?
Posted: Tue Oct 31, 2006 5:13 pm
by feyd
Code: Select all
preg_match('#' . preg_quote($pre,'#') . '(?:' . implode('|', array_map(create_function('$a', 'return preg_quote($a, \'#\');'), $pieces)) . preg_quote($post, '#') . '#i', $subject);
possibly. (untested)
Posted: Tue Oct 31, 2006 6:19 pm
by John Cartwright
Works like a charm feyder
Is there any particular reason I should have the pre and post preg_quotes in there? or was it just to demonstrate adding other parts to the regular expression?
Posted: Tue Oct 31, 2006 6:20 pm
by feyd
demonstrating adding other parts.
Posted: Tue Oct 31, 2006 6:21 pm
by John Cartwright
Oops I forgot to say Thank you

Posted: Tue Oct 31, 2006 8:33 pm
by John Cartwright
Code: Select all
protected function getPattern($pattern)
{
if (is_array($pattern) && count($pattern)) {
$pattern = implode('|', array_map(create_function('$a', 'return preg_quote($a, \'#\');'), $pattern));
}
$pattern = '#(?:' . $pattern .')#';
if ($this->sensitive) {
$pattern .= 'i';
}
/**
* We didn't want to actually escape asterisk's
*/
$pattern = str_replace('\*', '*', $pattern);
$pattern = str_replace('*', '.*?', $pattern);
return $pattern;
}
Hmm this is seemingly more complicated for my regex mind to figure out. I've been lurking through d11's course crashes but can't seem to grasp this.
At this time I am recieving
The array I'm giving it is
Code: Select all
$files = new ScanFiles(array('ind*', 'yoo', 'index.php'));
won't match index.php
From looking at d11's example in his crash course,
I can spot the difference between his ungreedy pattern in comparison to mine. I'm starting to get the feeling it's because the | characters.
Can anyone spot where I've gone wrong?

Posted: Tue Oct 31, 2006 8:58 pm
by feyd
Code: Select all
[feyd@home]>php -r "var_dump(preg_match('#(?:ind.*?|yoo|index\.php)#', 'index.php'));"
int(1)
Sure works for me.
Posted: Tue Oct 31, 2006 9:05 pm
by John Cartwright
Aha! Forgot to remove the preceding slash of the filename. basename() helps
Thanks again.