Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 695673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:59:01+00:00 2026-05-14T02:59:01+00:00

I want to be able to parse file paths like this one: /var/www/index.(htm|html|php|shtml) into

  • 0

I want to be able to parse file paths like this one:

 /var/www/index.(htm|html|php|shtml)

into an ordered array:

 array("htm", "html", "php", "shtml")

and then produce a list of alternatives:

/var/www/index.htm
/var/www/index.html
/var/www/index.php
/var/www/index.shtml

Right now, I have a preg_match statement that can split two alternatives:

 preg_match_all ("/\(([^)]*)\|([^)]*)\)/", $path_resource, $matches);

Could somebody give me a pointer how to extend this to accept an unlimited number of alternatives (at least two)? Just regarding the regular expression, the rest I can deal with.

The rule is:

  • The list needs to start with a ( and close with a )

  • There must be one | in the list (i.e. at least two alternatives)

  • Any other occurrence(s) of ( or ) are to remain untouched.

Update: I need to be able to also deal with multiple bracket pairs such as:

 /var/(www|www2)/index.(htm|html|php|shtml)

sorry I didn’t say that straight away.

Update 2: If you’re looking to do what I’m trying to do in the filesystem, then note that glob() already brings this functionality out of the box. There is no need to implement a custom solutiom. See @Gordon’s answer below for details.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:59:02+00:00Added an answer on May 14, 2026 at 2:59 am

    Non-regex solution 🙂

    <?php
    
    $test = '/var/www/index.(htm|html|php|shtml)';
    
    /**
     *
     * @param string $str "/var/www/index.(htm|html|php|shtml)"
     * @return array "/var/www/index.htm", "/var/www/index.php", etc
     */
    function expand_bracket_pair($str)
    {
        // Only get the very last "(" and ignore all others.
        $bracketStartPos = strrpos($str, '(');
        $bracketEndPos = strrpos($str, ')');
    
        // Split on ",".
        $exts = substr($str, $bracketStartPos, $bracketEndPos - $bracketStartPos);
        $exts = trim($exts, '()|');
        $exts = explode('|', $exts);
    
        // List all possible file names.
        $names = array();
    
        $prefix = substr($str, 0, $bracketStartPos);
        $affix = substr($str, $bracketEndPos + 1);
        foreach ($exts as $ext)
        {
            $names[] = "{$prefix}{$ext}{$affix}";
        }
    
        return $names;
    }
    
    function expand_filenames($input)
    {
        $nbBrackets = substr_count($input, '(');
    
        // Start with the last pair.
        $sets = expand_bracket_pair($input);
    
        // Now work backwards and recurse for each generated filename set.
        for ($i = 0; $i < $nbBrackets; $i++)
        {
            foreach ($sets as $k => $set)
            {
                $sets = array_merge(
                    $sets,
                    expand_bracket_pair($set)
                );
            }
        }
    
        // Clean up.
        foreach ($sets as $k => $set)
        {
            if (false !== strpos($set, '('))
            {
                unset($sets[$k]);
            }
        }
        $sets = array_unique($sets);
        sort($sets);
    
        return $sets;
    }
    
    var_dump(expand_filenames('/(a|b)/var/(www|www2)/index.(htm|html|php|shtml)'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using php to parse my xml file, all I want to be able
Background I want to be able to parse Javascript source in a Delphi Application.
I want to be able to do this. MyInterface interface = new ServiceProxyHelper<ProxyType>(); Here's
I am reading a text file from a URL and want to parse the
I want to parse some XML-file in Qt, but that file is located at
i want to parse a xhtml file and display in UITableView. what is the
Folks, I am using SIMPLEHTMLPARSER. I am not able to parse HTML, When i
I'm stuck not being able to parse irregularly embedded html tags. Is there a
I want to be able to capture the exception that is thrown when a
I want to be able to do: For Each thing In things End For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.