Page 1 of 1
should this work?
Posted: Thu Jul 22, 2004 12:38 pm
by bimo
Hi,
I am trying to build a file search engine. My question is this:
if the file names that I'm searching follow a specific pattern ($name###### where each # is exactly one digit), can I use regular expressions to match the file names like this
Code: Select all
<?php
preg_match_all("/[A-Z]+.\d.\d.\d.\d.\d.\d/i", (I don't know exactly what to put here in order to search all files in a folder), $shows);
?>
Thanks,
Posted: Thu Jul 22, 2004 12:45 pm
by feyd
that would match any alphabetic characters, followed by any character, a digit, any character, a digit, any character, a digit, any character, a digit, any character, a digit, any character, and a digit.
it'd match this too: 09871234lX6j1k3l1;1!01234798. If you want to save out each part, you'll need some () around each one you want to save..
you may want to add ^ and $ to the beginning and end respectively to tell the engine to match the entire string.
As for getting the files, you could use [php_man]glob[/php_man]() or a combo of [php_man]opendir[/php_man]()/[php_man]readdir[/php_man]() and using a while or foreach style loop...
Posted: Sun Jul 25, 2004 4:47 pm
by bimo
so I would use something like this:
Code: Select all
<?php
preg_match_all("/^[A-Z]+(.\d.\d)(.\d.\d)(.\d.\d)$/i", $shows);
?>
and $shows would be an array where every match would be a new element?
Posted: Sun Jul 25, 2004 5:06 pm
by feyd
$shows would be the string you want to test for matches... the next argument passed to preg_match_all would return the matches..
Posted: Sun Jul 25, 2004 9:54 pm
by bimo
right, sorry... I should've known that...
So what I did in the first version of the search is just an
Code: Select all
<?php
if(file_exists($filename)) { print("<a href="$filename.mp3">$filename</a>")}
?>
where $filename was created by form field elements in the POST array.
So would it now be something like
Code: Select all
<?php
preg_match_all("/^[A-Z]+(.\d.\d)(.\d.\d)(.\d.\d)$/i", $filename, $shows);
// and then to print the file names
$show_count = count($shows);
if($shows) { // if any matches were found and placed in $shows
for($i = 0; $i < $show_count; $i++) {
// loop through the $shows array and print a hyperlink to each...
print("<a href="$shows[$i].mp3">Show # $i</a>");
}
}
?>
That's some very rudimentary code, I'm sure, but is this the general idea of how it would work?
and thanks for all of your help. it is much appreciated.