Search code examples
phpregexglob

Search files using glob() and keywords


I have a bunch of files I want to search using the glob(pattern); function.
I also have a text box in an HTML form that will be used as keywords.

Currently my pattern looks like this:
glob("*.{avi,mkv,mp4,flv,mov}
But I need to replace the * with the text box value as multiple keywords.

How would I do that?


Solution

  • If the user enters a comma-separated list in the input box named keywords, you can use:

    glob('*{' . $_POST['keywords'] . "}*.{avi,mkv,mp4,flv,mov}", GLOB_BRACE);
    

    So if the user enters hello,bye, it will look for files that match the pattern

    *{hello,bye}*.{avi,mkv,mp4,flv,mov}