Putting together a glob search with wildcards

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
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Putting together a glob search with wildcards

Post by ozzy »

I've got the basis of it right, but i just dont know how to make it all work together. What im wanting to do is create a search using glob() to search a directory

I was thinking something like:

Code: Select all

$term = trim($_POST['search']);
$files = glob($dir."/files/*".$term."*");
And the '*' will work as the wildcard. So that's all working fine, but what do i do next. Someone suggested that i use "a loop, or use print_r()", but how do i do that? Something like:

Code: Select all

function printr ( $files , $term = '' ) {
Prob not, but that's all i could of. Any help? thanks in advance! :)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Code: Select all

print_r($files);
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Ah, cool. This is my script, but it doesnt seem to work, any suggestions?

Code: Select all

<form action="" method="POST"><input size="25" type="text" name="query" value="">&nbsp;&nbsp;<input type="submit" value="Search"></form>
<?php
$term = trim($_POST['search']);
$files = glob($dir."files/*".$term."*");
print_r($files);
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

maybe $_POST['query']?
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

still doesnt seem to change anything.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What's the new code and what are you using as input?
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

This:

Code: Select all

<form action="search.php" method="POST"><input size="25" type="text" name="query" value="">&nbsp;<input type="submit" value="Search"></form>
<?php
$files = glob($dir."files/*".$term."*");
$term = trim($_POST['query']);
print_r($files);
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$term is set after it's been used in glob()?
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Ooops..

i still get the problem when i move it...

Code: Select all

<form action="search.php" method="POST"><input size="25" type="text" name="query" value="">&nbsp;<input type="submit" value="Search"></form>
<?php
$term = trim($_POST['query']);
$files = glob($dir."files/*".$term."*");
print_r($files);
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have you verified what is being sent to glob()? I have yet to understand what "problem" you're actually having. Maybe you need to adjust what you are sending to glob?
Post Reply