Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>New answer after OP edited his question</em></p> <p>From your edited question, I understand you want to dynamically populate a <code>SelectBox</code> element on an HTML webpage with the files found in a certain directory for <code>option value</code>. The values are supposed to be split by dash, underscore and number to provide the <code>option name</code>, e.g.</p> <pre><code>Directory with Files &gt; SelectBox Options filename1.txt &gt; value: filename1.txt, text: Filename 1 file_name2.txt &gt; value: filename1.txt, text: File Name 2 file-name3.txt &gt; value: filename1.txt, text: File Name 3 </code></pre> <p>Based from the code I gave in my other answer, you could achieve this with the <code>DirectoryIterator</code> like this:</p> <pre><code>$config = array(); $dir = new DirectoryIterator('.'); foreach($dir as $item) { if($item-&gt;isFile()) { $fileName = $item-&gt;getFilename(); // turn dashes and underscores to spaces $longFileName = str_replace(array('-', '_'), ' ', $fileName); // prefix numbers with space $longFileName = preg_replace('/(\d+)/', ' $1', $fileName); // add to array $config[] = array('short' =&gt; $filename, 'long' =&gt; $longFilename); } } </code></pre> <p>However, since filenames in a directory are unique, you could also use this as an array:</p> <pre><code>$config[$filename] =&gt; $longFilename; </code></pre> <p>when building the config array. The short filename will form the key of the array then and then you can build your selectbox like this:</p> <pre><code>foreach($config as $short =&gt; $long) { printf( '&lt;option value="%s"&gt;%s&lt;/option&gt;' , $short, $long); } </code></pre> <p>Alternatively, use the Iterator to just create an array of filenames and do the conversion to long file names when creating the Selectbox options, e.g. in the <code>foreach</code> loop above. In fact, you could build the entire SelectBox right from the iterator instead of building the array first, e.g.</p> <pre><code>$dir = new DirectoryIterator('.'); foreach($dir as $item) { if($item-&gt;isFile()) { $fileName = $item-&gt;getFilename(); $longFileName = str_replace(array('-', '_'), ' ', $fileName); $longFileName = preg_replace('/(\d+)/', ' $1', $fileName); printf( '&lt;option value="%s"&gt;%s&lt;/option&gt;' , $fileName, $longFileName); } } </code></pre> <p>Hope that's what your're looking for. I strongly suggest having a look at the chapter titled <a href="http://www.php.net/manual/en/" rel="nofollow noreferrer">Language Reference in the PHP Manual</a> if you got no or very little experience with PHP so far. There is also a free online book at <a href="http://www.tuxradar.com/practicalphp" rel="nofollow noreferrer">http://www.tuxradar.com/practicalphp</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload