Current Location: Home> Latest Articles> How does PHP's parse_url function parse URLs containing anchors (fragment)? How will it handle the anchor part in the URL?

How does PHP's parse_url function parse URLs containing anchors (fragment)? How will it handle the anchor part in the URL?

gitbox 2025-05-28

When doing web development, we often need to extract useful information from URLs, such as protocols, hosts, paths, query parameters, etc. PHP's built-in parse_url function provides a convenient way to parse these parts of a URL. This article will focus specifically on how this function behaves when processing URLs with fragments.

1. What is an anchor point (fragment)?

An anchor point in a URL (also called "fragment identifier") is a part that starts with # and is used to point to a location in an HTML document. For example:

 https://gitbox.net/articles/php?query=test#section2

#section2 here is the anchor point in the URL, which will not be sent to the server, but will be used by the browser to locate the specific location in the document.

2. Introduction to parse_url function

The basic usage of PHP's parse_url function is as follows:

 parse_url(string $url, int $component = -1): mixed
  • $url : The URL string to parse;

  • $component : Optional parameter, if provided, the specified part will be returned (such as PHP_URL_HOST , PHP_URL_PATH , etc.);

  • The return value is an associative array containing different parts of the URL.

3. Analysis example of URL with anchor point

We actually test it with a URL containing anchors:

 $url = "https://gitbox.net/path/page.php?foo=bar#top";
$parsed = parse_url($url);
print_r($parsed);

The operation results are as follows:

 Array
(
    [scheme] => https
    [host] => gitbox.net
    [path] => /path/page.php
    [query] => foo=bar
    [fragment] => top
)

As can be seen from the result, parse_url successfully recognizes the fragment part and returns its value top with the fragment key.

4. Only extract the anchor point part

If you only care about the anchor part, you can use the PHP_URL_FRAGMENT constant:

 $fragment = parse_url($url, PHP_URL_FRAGMENT);
echo $fragment; // Output:top

This is great for quickly locate anchor information in URLs without manually parsing strings.

V. Other precautions

  1. Anchor points will not affect paths or query resolution <br> The anchor part appears after the path and query parameters, and it does not cause any interference to the parsing results of path or query .

  2. The fragment will not appear in the array without anchor points <br> If there is no anchor in the URL, the returned array will not contain the fragment key.

  3. No URL decoding is performed
    parse_url only performs string parsing and does not perform URL decoding for each part. If you need to read Chinese or encoded characters, you need to use urldecode() to further process it.

6. Summary

parse_url is a powerful tool for handling URLs in PHP, which accurately recognizes and extracts the anchor point (fragment) part. Through this function, developers can easily disassemble URLs and obtain useful information, improving the readability and maintainability of their code. Especially when dealing with URLs with positioning information or front-end navigation, correctly parsing anchor points can help us more accurately control page behavior or jump logic.

  • Related Tags:

    URL