How can PHP be used to dynamically sort images based on category in a file name?

To dynamically sort images based on category in a file name using PHP, you can create an array to store the categories and then loop through the images to categorize them accordingly. You can use functions like `scandir()` to read the files in a directory and `preg_match()` to extract the category from the file name.

$categories = array("nature", "animals", "architecture");

$images = scandir("path/to/images");

foreach($images as $image) {
    if($image != "." && $image != "..") {
        preg_match("/\[(.*?)\]/", $image, $matches);
        $category = $matches[1];
        
        if(in_array($category, $categories)) {
            // Move or display the image based on its category
        }
    }
}