Login | Contact Us | Site Map | Resources

PHP Functions

Home > PHP Tutorial > PHP Url Functions

PHP Url Functions

PHP provides several functions used to deal with URL strings. A URL is an address used to identify a website, and a URL string might look something like this: http://www.phpforkids.com/php/php-functions-url.php

The URL functions that we will learn about here are:

Function Description
get_headers() Fetches all the headers sent by the server in response to a HTTP request
get_meta_tags() Extracts all meta tag content attributes from a file and returns an array
parse_url() Parse a URL and return its components
urlencode() URL-encodes string
urldecode() Decodes URL-encoded string
rawurlencode() URL-encode according to RFC 1738
rawurldecode() Decode URL-encoded strings

Am I the only one that wants all that translated into English?! Let's begin with get_headers(), which will return an array containing the headers sent by the server in response to an HTTP request.

Example Code:
<?php
  $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  // Example: http://www.phpforkids.com/php/php-functions-url.php
  $headers = get_headers($url);
  print_r($headers);
?>

Result (Array Created):
  Array (
    [0] => HTTP/1.1 200 OK
    [1] => Date: Sun, 05 Sep 2010 22:30:39 GMT
    [2] => Server: Apache/2.2.3 (CentOS)
    [3] => X-Powered-By: PHP/5.1.6
    [4] => Set-Cookie: PHPSESSID=ggnc33i24gukchi5iqnqg8b4a7; path=/
    [5] => Expires: Thu, 19 Nov 1981 08:52:00 GMT
    [6] => Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    [7] => Pragma: no-cache
    [8] => Vary: Accept-Encoding
    [9] => Content-Length: 6746
    [10] => Connection: close
    [11] => Content-Type: text/html
    [12] => Content-Language: en
  )

..ooookkkkaaayyy... moving on (to something more useful?), the get_meta_tags() function returns an array containing the meta tags found between the HTML <head> and </head> tags of the specified webpage.

Example Code:
<?php
  $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  // Example: http://www.phpforkids.com/php/php-functions-url.php
  $MetaTags = get_meta_tags($url);
  print_r($MetaTags);
?>

Result (Array Created):
  Array (
    [description] => Free Tutorial On PHP Url Functions At PHP For Kids.com
    [keywords] => php, tutorial, learn, free, url, functions
  )

The parse_url() functions takes a url and returns it to you in little pieces stored in an array.

Example Code:
<?php
  $url = "http://www.phpforkids.com/php/php-functions-url.php?question=testing-testing";
  $ParsedUrl = parse_url($url);
  print_r($ParsedUrl);
?>

Result (Array Created):
  Array (
    [scheme] => http
    [host] => www.phpforkids.com
    [path] => /php/php-functions-url.php
    [query] => question=testing-testing
  )

The urlencode() and rawurlencode() functions both encode strings by replacing all non-alphanumeric characters such as spaces and symbols with placeholders that can later be decoded to return the original string. These functions are ideal for preserving strings used in URL queries, or even passed between pages as "GET" data when a form is submitted.

The urldecode() and rawurldecode() functions can be used to decode the strings once they reach their destination and need to be used or displayed in their original format.

The urlencode() function works with urldecode() and the rawurlencode() function works with rawurldecode(). The difference between the two sets of functions is only in the style that they used to encode each string.

Example Code:
<?php
  $string1 = "What is a mouse's favorite game?";
  $result = urlencode($string1);
  echo $result . "<br />";
  $result = urldecode($string1);
  echo $result . "<br />";

  $string2 = "Hide and squeak!";
  echo rawurlencode($string2);
  echo rawurldecode($string2);
?>

Result of urlencode() and urldecode():
  What+is+a+mouse%27s+favorite+game%3F
  What is a mouse's favorite game?

Result of rawurlencode() and rawurldecode():
  Hide%20and%20squeak%21
  Hide and squeak!