What is the best way to extract parts of a string in PHP, such as directories from a URI stored in a database?

To extract parts of a string in PHP, such as directories from a URI stored in a database, you can use the `parse_url()` function to break down the URL into its components and then extract the desired parts. Specifically, you can use the `path` component to get the directories from the URI.

// Example URI stored in a database
$uri = "https://www.example.com/dir1/dir2/page.php";

// Parse the URI and extract the path component
$parsed_url = parse_url($uri);
$path = $parsed_url['path'];

// Split the path into an array of directories
$directories = explode('/', $path);

// Output the directories
print_r($directories);