What are some potential pitfalls when trying to extract the current directory from a URL in PHP?

When trying to extract the current directory from a URL in PHP, a potential pitfall is that the URL might not always have a consistent structure, making it difficult to accurately extract the directory. To solve this issue, you can use the `parse_url()` function to break down the URL into its components and then extract the directory from the path component.

$url = "https://www.example.com/path/to/page.php";
$path = parse_url($url, PHP_URL_PATH);
$directory = dirname($path);
echo $directory;