Are there any best practices for handling URLs with anchors in PHP scripts?

When handling URLs with anchors in PHP scripts, it's important to properly parse the URL and extract the anchor part separately if needed. One common approach is to use the `parse_url()` function to break down the URL into its components and then extract the anchor part using string manipulation functions like `strpos()` and `substr()`.

$url = "http://example.com/page#section";
$parts = parse_url($url);

if(isset($parts['fragment'])){
    $anchor = $parts['fragment'];
    $url = str_replace("#".$anchor, "", $url); // Remove anchor from URL
}

echo "URL without anchor: " . $url . "<br>";
echo "Anchor: " . $anchor;