Hi all,

I've tried searching both here and google for help on this one. I'm not quite sure what to call it basically I have a directory with different files all .txt. I want to display only the ones beginning with letter A. How do I modify my below code to do this?

<?php
if ($handle = opendir('poems/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "$file\n <br />";
        }
    }
    closedir($handle);
}
?>

I know I need to use something like A* but I don't even know what to call it to search for it?

Thanks!

Dani AI

Generated

A few concise alternatives and caveats that complement ’s suggestion.

For a quick, simple match use PHP’s glob — it accepts wildcards and can filter the extension at the same time. It returns an array of matching paths, so you can print basenames and escape output to avoid XSS:

<?php
$matches = glob('poems/A*.txt');          // uppercase A only
$matches = glob('poems/{A*,a*}.txt', GLOB_BRACE); // A or a (GLOB_BRACE not available everywhere)
foreach ($matches as $path) {
    echo htmlspecialchars(basename($path)) . "<br>\n";
}

If you need regex power (case-insensitive, more complex rules) use scandir() + preg_grep() or iterate with DirectoryIterator for large folders (lower memory overhead):

<?php
$it = new DirectoryIterator('poems');
foreach ($it as $finfo) {
    if ($finfo->isFile() && stripos($finfo->getFilename(), 'A') === 0) {
        echo htmlspecialchars($finfo->getFilename()) . "<br>\n";
    }
}

Notes and gotchas: ’s string-index technique is fine for ASCII filenames, but it’s case-sensitive and breaks for multibyte names — use mb_substr or stripos/strncasecmp for Unicode/case-insensitive checks. glob() and scandir() return arrays (higher memory use) while DirectoryIterator yields entries one-by-one. Always validate isFile() when appropriate, and escape output with htmlspecialchars() before printing filenames.

Manuals: glob() docs (https://www.php.net/manual/en/function.glob.php) and DirectoryIterator (https://www.php.net/manual/en/class.directoryiterator.php) have useful examples and portability notes.

Recommended Answers

All 3 Replies

Hi all,

I've tried searching both here and google for help on this one. I'm not quite sure what to call it basically I have a directory with different files all .txt. I want to display only the ones beginning with letter A. How do I modify my below code to do this?

<?php
if ($handle = opendir('poems/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "$file\n <br />";
        }
    }
    closedir($handle);
}
?>

I know I need to use something like A* but I don't even know what to call it to search for it?

Thanks!

<?php
if ($handle = opendir('poems/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && $file[0] == 'A') {
            echo "$file\n <br />";
        }
    }
    closedir($handle);
}

Thanks for the quick reply ShawnCplus. It spares me an awful lot of pain and hair ripping! Thank you so much!

Thanks for the quick reply ShawnCplus. It spares me an awful lot of pain and hair ripping! Thank you so much!

As a short explanation strings are arrays of characters so if you want to get the 3rd letter in a string just do $string[2] (counting begines at 0)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.