should this work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
bimo
Forum Contributor
Posts: 100
Joined: Fri Apr 16, 2004 11:18 pm
Location: MD

should this work?

Post 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,
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
User avatar
bimo
Forum Contributor
Posts: 100
Joined: Fri Apr 16, 2004 11:18 pm
Location: MD

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
bimo
Forum Contributor
Posts: 100
Joined: Fri Apr 16, 2004 11:18 pm
Location: MD

Post 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.
Post Reply