The task:

Create a list that stores the parsing of the URL according to its properties: protocol, port, host, path, resource, query words and print as “Name / Value”

I use the parse_url function, but I don’t understand a bit, I recently opened PHP. Can anyone explain to me about parsing the URL: what is "protocol, port, host, path, resource, query words"? The protocol, it seems, is http, the host is understandable, I get lost about the rest. They can be parse_url using parse_url and what should be the address? If possible, explain with examples.

At the given URL

http://www.scriptol.com/how-to/parsing-url.php#content

It gives me only

Array ([scheme] => http [host] => www.scriptol.com [path] => /how-to/parsing-url.php [fragment] => content)

PHP code:

  $url = "http://www.scriptol.com/how-to/parsing-url.php#content"; $arr = parse_url($url); print_r($arr); 

    1 answer 1

    As an example, consider the link of the form:

    http: // shurik: qwerty@my.site.com: 9191 / documents.php? documentId = 128 & type = pdf # content

    The parse_url function returns an array of data in which the following keys can be:

    • scheme - well, it is clear here, http or https or something else
    • host - it's clear here too
    • port - the port to which you connect (in url is written after the host, separated by a colon (in this case, it is 9191))
    • user - usually not used, but in this case the user will be shurik. It is recorded at the beginning of the host.
    • pass - password, recorded with user in this case qwerty
    • path - path to the requested file. In this case: /documents.php (always starts with /)
    • query - query parameters are written to the url after the "?" in this case, documentId = 128 & type = pdf
    • fragment is a pointer to an anchor, in this case content. Recorded after #

    Hope this helped.

    PS: >> gives me only

    well, right, because you have no other arguments in your url

    • thanks) I just didn't quite understand what it is - gvenog
    • Now, in addition to this, I was asked to make the output not a string like this: Array ([scheme] => http [host] => www.scriptol.com [path] => /how-to/parsing-url.php [fragment] => content) and in a column with Russian names Protocol => http, etc. Do these names change in the parse_url function itself? How to change the keys in it? - gvenog
    • one
      just make your array $ url = parse_url ('...'); $ arr = array ('Protocol' => $ url ['scheme'], 'Host' => $ url ['host'], // and so on); Well, well, you can display it like this: foreach ($ arr as $ key => $ val) echo $ key. ' = '. $ val.' <br /> '; - Sh4dow
    • thanks again) - gvenog
    • one
      I would recommend making an array of translations: $ t = array ('scheme' => 'Protocol', 'host' => 'Host', ...); $ url = parse_url ('...'); foreach ($ url as $ k => $ v) {echo $ t [$ k]. ':'. $ v. '<br/>'; } - Alex Kapustin