hi,

how can i select only unique string out of a series of iterating strings?
for example, i have this folder which contains number of files with quite long names.
i have used readdir() to find all the files.
assume that the name of the file has an format.
eg: "some_name_string(a_string)(a_date).html"
now when i iterate these names i want to display pages which has (a_string) value a unique one. like using DISTINCT in database,(distinct (a_string) value)
how can i d this.. i have no idea. Please help me out,
thanks in advance.

Dani AI

Generated

As discovered, you don't need a custom function — PHP has array_unique(). For your filename pattern like some_name_string(a_string)(a_date).html the usual pipeline is: get the file list, extract the parenthetical token you want, normalize it if necessary, then dedupe with array_unique() (and array_values() to reindex).

Here is a compact example using glob() and a regex to grab the first (...) group:

$files = glob('/path/to/dir/*.html');
$tokens = [];

foreach ($files as $f) {
    $name = basename($f);
    if (preg_match_all('/\(([^)]*)\)/', $name, $m)) {
        $tokens[] = trim($m[1][0]); // change index if you need the 2nd group
    }
}

$unique = array_values(array_unique($tokens));

If you prefer streaming (avoid building large intermediate arrays) or want case-insensitive uniqueness:

$seen = [];
$unique = [];

foreach ($files as $f) {
    if (preg_match('/\(([^)]*)\)/', basename($f), $m)) {
        $key = mb_strtolower(trim($m[1])); // normalize
        if (!isset($seen[$key])) {
            $seen[$key] = true;
            $unique[] = $m[1];
        }
    }
}

Notes and troubleshooting:

  • array_unique() is case-sensitive and preserves the first occurrence — use array_values() to get a 0-based numeric array.
  • Normalize (trim(), mb_strtolower()) if filenames vary in spacing or case.
  • If the second parentheses always contains a date with a fixed format, tighten the regex (or match the date explicitly) to be more robust.
  • If you're already using readdir(), the extraction logic is the same; consider DirectoryIterator or glob() for clearer code and automatic extension filtering.

ok i found it myself , unique_array();

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.