Search code examples
phpregexurlurlparse

Getting parts of a URL in PHP


How can I extract the following parts using PHP function:

  • The Domain
  • The path without the file
  • The file
  • The file with Extension
  • The file without Extension
  • The scheme
  • The port
  • The query
  • The fragment
  • (add any other that you think would be useful)

Ex.1 https://stackoverflow.com/users/test/login.php?q=san&u=post#top

  • The Domain (stackoverflow.com)
  • The path without the file (/users/test/)
  • The file(login.php)
  • The file Extension (.php)
  • The file without Extension (login)
  • The scheme(https:)
  • The port(return empty string)
  • The query(q=san&u=post)
  • The fragment(top)

Ex: 2 stackoverflow.com/users/test/login.php?q=san&u=post#top

  • The Domain (stackoverflow.com)
  • The path without the file (/users/test/)
  • The file(login.php)
  • The file Extension (.php)
  • The file without Extension (login)
  • The scheme(return empty string)
  • The port(return empty string)
  • The query(q=san&u=post)
  • The fragment(top)

Ex: 3 /users/test/login.php?q=san&u=post#top

  • The path without the file (/users/test/)
  • The file(login.php)
  • The file Extension (.php)
  • The file without Extension (login)
  • The query(q=san&u=post)
  • The fragment(top)
  • For remaining (return empty string)

Ex: 4 /users/test/login?q=san&u=post#top

  • The path without the file (/users/test/)
  • The file(login)
  • The file Extension (return empty string)
  • The file without Extension (login)
  • The query(q=san&u=post)
  • The fragment(top)
  • For remaining (return empty string)

Ex: 5 login?q=san&u=post#top

  • The file(login)
  • The file Extension (return empty string)
  • The file without Extension (login)
  • The query(q=san&u=post)
  • The fragment(top)
  • For remaining (return empty string)

Ex: 6 ?q=san&u=post

  • The query(q=san&u=post)
  • For remaining (return empty string)

I checked parse_url function, but doesn't return what I need. Since, I'm beginner in PHP, it was difficult for me. If you have any idea, please answer.

Thanks in advance.


Solution

  • PHP provides a parse_url function.

    This function parses a URL and returns an associative array containing any of the various components of the URL that are present.

    This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial URLs are also accepted, parse_url() tries its best to parse them correctly.


    You can see the test cases executed here.

    $urls = array(
      "https://stackoverflow.com/users/test/login.php?q=san&u=post#top",
      "/users/test/login.php?q=san&u=post#top",
      "?q=san&u=post#top",
      "login.php?q=san&u=post#top",
      "/users/test/login?q=san&u=post#top",
      "login?q=san&u=post#top"
    );
    foreach( $urls as $x ) {
      echo $x . "\n";
      var_dump( parse_url($x) );
    }