Help using SCANDIR / GLOB

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
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Help using SCANDIR / GLOB

Post by bla5e »

In my /home folder on the server I have a bunch of other folers. I want to populate a select/option menu with the FOLDERS from /home. Not all the folders need to be listed though, only the ones that are in this format text_text.com
What is actually happening is the folders are e-mail address with _ in replace of the '@' sign and i want the user to beable to select the e-mail address from the option/select box. How do I accomplish this? When I use scandir, i get all the folders (including ., ..) Any help please?!
Last edited by bla5e on Thu Jun 17, 2010 8:56 am, edited 1 time in total.
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: Help using SCANDIR

Post by bla5e »

Cliff note: how do i filter the results that are returned with scandir? trying to eliminate ., .., (which i have done) and anything that isnt in this format text_text.com
Last edited by bla5e on Wed Jun 16, 2010 4:48 pm, edited 1 time in total.
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: Help using SCANDIR

Post by bla5e »

This is my current code, I am using Smarty as a template engine.

Smarty HTML file

Code: Select all

<select name="domains" id="domains" MULTIPLE>
{foreach from=$files item=email}
  {if $email != '..' && $email != '.'}
    <option value="">{$email}</option>
  {/if}
{foreachelse}
<option>THIS IS AN ERROR!</option>
{/foreach}
</select>

Smarty PHP file

Code: Select all

$files = glob("/home/*_*");
I just need to use a regex/string replace _ with @ and remove the preceding /home
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: Help using SCANDIR / GLOB

Post by bla5e »

Any help using preg replace with glob/scandir??!
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: Help using SCANDIR / GLOB

Post by bla5e »

Solution:

Code: Select all

$files = glob("/home/*_*");
  $replace = array('/\/home\//', '/_/');
  $with = array('', '@');
  foreach($files as $file) {
    $file = preg_replace($replace, $with, $files);
  }
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help using SCANDIR / GLOB

Post by AbraCadaver »

I see you solved it yourself, however here is an alternative without regex:

Code: Select all

foreach(glob("/home/*_*") as $file) {
    $files[] = str_replace('_', '@', basename($file));
}
Or for fun:

Code: Select all

foreach(array_map('basename', glob("/home/*_*")) as $file) {
   $files[] = str_replace('_', '@', $file);
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply