10 PHP Url Functions – Every beginner must check

URL (Uniform Resource Locator) often termed as web address is a unique reference of web resource that is accessible on the internet.To visit any website user go to the browser (chrome, firefox, safari) and type any website name like wdb24.com and browser loads requested website.But URLs are not restricted with http/https. URLs are also used in ftp (file transfer protocol), email (mailto), database access and many other applications.A typical URL look something like this.

If you want read more about URL then visit https://en.wikipedia.org/wiki/URL

In PHP, there are bundle of functions which handle URL and its fragments quite well. So in this post, I am going to show you 10 PHP built in functions and their use.

PHP Built in URL Functions:

1) parse_url():

This function parses the url and return its components. It is a very handy function and use frequently in php projects.

Syntax: parse_url(string $url, optional $component)

$url URL to parse
$component Specify any Component otherwise blank will return an array. Components are PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT

parse_url example 1:

The output of the above code is below. Parse url split url into 2 indices. First one is scheme which is http and second is a host name which is www.example.com

Output:

parse_url example 2:

In the above code, a very long url passed in parse_url function and parse_url split url into different indexes of array.

Output:

parse_url example 3:

In the above example ftp url with user, password and file path pass in the parse_url and parse_url split ftp url into different indexes.

Output:



2) get_headers():

This function fetch array (indexed or associative) of all the headers send by the server in response to a HTTP request.

Syntax: get_headers( string $url [, int $format = 0 ] )

$url Target url
$format is an optional parameter. If non-zero value pass in $format then it returns array with string keys

get_header example 1:

In the above snippet I created $url and pass Wikipedia main page link. $getHeader variable calls get_header function with $url. You may notice that I haven’t pass second argument in get_header function. In the output it returns indexed array.

Output:

get_header example 2:

Above is same as example 1 except I passed 1 as a second parameter in get_headers. Now it returns same array as example 1 but in array will be associative.

Output:


3) get_meta_tags():

This function extracts all meta tag content attributes from a file and returns an array.

Syntax: parse_url(string $filename, bool $use_include_path = false)

$filename The path to the HTML file, as a string. This can be a local file or an URL
$use_include_path Set use_include_path to TRUE will result in PHP trying to open the file along the standard include path as per the include_path directive. This is used for local files, not URLs
Note: get_meta_tags only reads meta tags line by line and it stops on closing head tag

get_meta_tags example 1:

In the above code, I passed wdb24.com url into get_meta_tags function and it will print all meta tags of wdb24.com page in a associative array.

Output:


4) http_build_query():

This function creates a URL-encoded query string from the associative (or indexed) array provided. This function is mostly used in cURL.

Syntax: http_build_query($query_data [, string $numeric_prefix [, string $arg_separator [, int $enc_type = PHP_QUERY_RFC1738 ]]] )

$query_data single or multi-dimensional array.It can be object too, if it is an object only public properties will be incorporated.
$numeric_prefix If numeric indices are used in the base array and this parameter is provided, it will be prepended to the numeric index for elements in the base array only.
$arg_separator is used to separate arguments but may be overridden by specifying this parameter.
$enc_type By default, PHP_QUERY_RFC1738

http_build_query example 1:

I passed indexed array to http_build_query function and it will return url-encoded string as below.

Output:

http_build_query example 2:

Above code is almost same like example 1 but in http_build_query I passed second parameter as “var_”. Now this function use var_ as a prefix of url-encoded keys.

Output:

http_build_query example 3:

$array3 is an associative array and when I passed $array3 to http_build_query. It will generate below url-encoded string.

Output:

http_build_query example 4:

In the above example $array4 is an array with mixed values. First 2 indices of $array4 has no keys and rest of three have keys. Then in the next line I passed “my_var” as a second parameter in http_build_query. “my_var” will only apply to first 2 indices of $array4 because they don’t have any key define.

Output:


5) urlencode():

This function encode string. This function is mostly use in url, as a convenient way to pass variable to next page.

Syntax: urlencode( string $str )

$str The string to be encoded

urlencode example 1:

Output:


6) urldecode():

This function decodes url encoding string.Decodes any %## encoding in the given string. Plus symbols (‘+’) are decoded to a space character.

Syntax: urldecode( string $str )

$str The string to be decoded

urldecode example 1:

Output:



7) rawurlencode():

This function encode url according to RFC 3986.

Syntax: rawurlencode( string $str )

$str Url to be encoded
Note: Prior to PHP 5.3.0, rawurlencode encoded tildes (~) as per » RFC 1738.

rawurlencode example 1:

Output:

rawurlencode example 2:

Output:


8) rawurldecode():

This function decode url encoded string

Syntax: rawurldecode( string $str )

$str Url to be decoded
Note: rawurldecode() does not decode plus symbols (‘+’) into spaces. urldecode() does

rawurldecode example 1:

Output:

rawurldecode example 2:

Output:


9) base64_encode():

This function encode data with MIME base64. This function take 33% more space than original data.

Syntax: base64_encode( string $data )

$data Data to encode

base64_encode example 1:

Output:


10) base64_decode():

This function decodes data with MIME base64

Syntax: base64_decode( string $data [, bool $strict = false ] )

$data Encoded data
$strict Default value set for $strict is false. If this value change to true, then it will return false if character in encoded data are not base64 alphabet. Otherwise invalid characters will be silently discarded.

base64_decode example 1:

Output:

This text will convert into base64 then it will decode.

Also read:

 

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *