Page 1 of 1

Help using SCANDIR / GLOB

Posted: Wed Jun 16, 2010 4:35 pm
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?!

Re: Help using SCANDIR

Posted: Wed Jun 16, 2010 4:38 pm
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

Re: Help using SCANDIR

Posted: Wed Jun 16, 2010 4:47 pm
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

Re: Help using SCANDIR / GLOB

Posted: Thu Jun 17, 2010 8:56 am
by bla5e
Any help using preg replace with glob/scandir??!

Re: Help using SCANDIR / GLOB

Posted: Thu Jun 17, 2010 9:06 am
by bla5e
Solution:

Code: Select all

$files = glob("/home/*_*");
  $replace = array('/\/home\//', '/_/');
  $with = array('', '@');
  foreach($files as $file) {
    $file = preg_replace($replace, $with, $files);
  }

Re: Help using SCANDIR / GLOB

Posted: Thu Jun 17, 2010 12:28 pm
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);
}