NOVINKA: Najžiadanejšie rekvalifikačné kurzy teraz s 50% zľavou + kurz AI ZADARMO. Nečakaj, táto ponuka dlho nevydrží! Zisti viac:

PHP - parse_url

Trieda

Koreň \ Bez triedy

Metóda - parse_url

(PHP 4, PHP 5, PHP 7)

The function is used for dividing a URL adress into parts which are returned as an associative array.

Note: The function should not be used for validating a URL. It just divides a URL into it's parts, see the example below.

Procedurálne

  • function parse_url (string $url, int $component = -1) : mixed

Parametre

NázovDátový typPredvolená hodnotaPopis
$urlstring

The input URL address to be processed.

$componentint -1

If the parameter is specified, the function returns only the specified part of the URL address.

It can contain the value of one of the following constants:

  • PHP_URL_SCHEME
  • PHP_URL_HOST
  • PHP_URL_PORT
  • PHP_URL_USER
  • PHP_URL_PASS
  • PHP_URL_PATH
  • PHP_URL_QUERY
  • PHP_URL_FRAGMENT

See the first example for the description for each part.

Mávratovej hodnoty

Vracia: mixed

Returns false if the URL is malformed.

Returns an associative array with parts of the given URL address. If the URL doesn't contain some part (e.g. the user), the array doesn't contain that key.

If a particular part is specified (by the second parameter), it returns a string with this value (if the port is requested, it returns the int type), or null if the URL doesn't contain that part.

Príklady

An example of URL address parts:

print_r(parse_url('http://john:[email protected]:80/php/basics?view=all&hide=nothing#container'));

If the URL doesn't contain some part (e.g. the user), the array doesn't contain that key.

print_r(parse_url('http://ict.social/php/basics'));

Getting only a defined part of a URL:

<?php
$url = 'http://ict.social:80/php/basics?view=all&hide=nothing#container';

var_dump(parse_url($url, PHP_URL_HOST)); // string
var_dump(parse_url($url, PHP_URL_PORT)); // int
var_dump(parse_url($url, PHP_URL_USER)); // null (URL doesn't contain the user)

Súvisiace manuály

      • function parse_str (string $encoded_string, array &$result) : void
      Aktivity