I want to split each components of a url and echo it separately. Like http://www.example.com/username Now i want to split each part of url and echo it as http, Www, Example, Com, Username.
Does the example here help? http://php.net/manual/en/function.parse-url.php
<?php
$url = 'http://username:password@hostname:9090/path?arg=value#anchor';
var_dump(parse_url($url));
var_dump(parse_url($url, PHP_URL_SCHEME));
var_dump(parse_url($url, PHP_URL_USER));
var_dump(parse_url($url, PHP_URL_PASS));
var_dump(parse_url($url, PHP_URL_HOST));
var_dump(parse_url($url, PHP_URL_PORT));
var_dump(parse_url($url, PHP_URL_PATH));
var_dump(parse_url($url, PHP_URL_QUERY));
var_dump(parse_url($url, PHP_URL_FRAGMENT));
?>
Edit after the comments:
<?php
$url = 'http://www.example.com/username';
$scheme=parse_url($url, PHP_URL_SCHEME);
$path=parse_url($url, PHP_URL_PATH);
$path2 = explode("/", $path);
$host=parse_url($url, PHP_URL_HOST);
echo "$scheme",", ";
$pieces = explode(".", $host);
echo "$pieces[0]",", ";
echo "$pieces[1]",", ";
echo "$pieces[2]",", ";
echo "$path2[1]","
";
?>
Should yield:
http, www, example, com, username
You can do it by using parse_url() function.
mixed parse_url ( string $url [, int $component = -1 ] )
You will find the documentation here: http://php.net/manual/de/function.parse-url.php
Hope that helps for you.
If you could find a regular expression which would filter on . / ? = and :// I think you would be able to solve your problem with this: http://php.net/manual/en/function.preg-split.php