Jump to content

getting the path


phpmady

Recommended Posts

Trust me when I say I spent lots of time making this, I even have more code that gets the main parsed host from subdomains, checks for correct http and so on, I tried to simplify it all into the below code so everyone can use what they need from it.

 

<?php
$url = "http://www.phpfreaks.com/forums/php-coding-help/?action=post";

function getparsedHost($new_parse_url) {
                $parsedUrl = parse_url(trim($new_parse_url));
                return trim($parsedUrl[host] ? $parsedUrl[host] : array_shift(explode('/', $parsedUrl[path], 2)));
} 
$new_parse_url = $url;            
            $get_parse_url = parse_url($new_parse_url, PHP_URL_HOST);
            $host_parse_url .= str_replace(array('Www.','WWW.'), '', $get_parse_url);
            $host_parse_url = strtolower($host_parse_url);
            $port_parse_url = parse_url($new_parse_url, PHP_URL_PORT);
            $user_parse_url = parse_url($new_parse_url, PHP_URL_USER);
            $pass_parse_url = parse_url($new_parse_url, PHP_URL_PASS);
            $get_path_parse_url = parse_url($new_parse_url, PHP_URL_PATH);                     
            $path_parts = explode("/",$get_path_parse_url);
            $extract_path_1 = $path_parts[1];
            $extract_path_2 = $path_parts[2];
            $extract_path_3 = $path_parts[3];
            $path_parse_url .= str_replace(array('Www.','WWW.'), '', $get_path_parse_url);
            $query_parse_url = parse_url($new_parse_url, PHP_URL_QUERY);
            $query_parse_url = "?$query_parse_url";
            $query_parse_url = rtrim($query_parse_url, '#');
            $fragment_parse_url = parse_url($new_parse_url, PHP_URL_FRAGMENT);
            $fragment_parse_url = "#$fragment_parse_url";
            $hostpath_url = "$host_parse_url$path_parse_url";
            $hostpath_url = rtrim($hostpath_url, '?');
            $query_parse_url = rtrim($query_parse_url, '?');

            $hostpathquery_url = "$host_parse_url$path_parse_url$query_parse_url";

            $complete_url = "$host_parse_url$port_parse_url$user_parse_url$pass_parse_url$path_parse_url$query_parse_url$fragment_parse_url";
            $complete_url = rtrim($complete_url, '#');
//adding http back to front, can do substr checks in url to suit your needs
$complete_url = "http://$complete_url";        

$parsed = getparsedHost($new_parse_url);          
echo "url: $url<br />";
echo "parsed: $parsed <br />";
echo "host parse: $host_parse_url<br />";
echo "path parse: $path_parse_url<br />";
echo "extract path 1: $extract_path_1<br />";
echo "extract path 2: $extract_path_2<br />";
echo "extract path 3: $extract_path_3<br />";
echo "host and path: $hostpath_url<br />";
echo "complete: $complete_url<br />";

?>

Link to comment
Share on other sites

I made a simpler function exact to your needs.

 

I'm sure can be added or improved upon, but it's a great start for you and works.

 

<?php

function getPathOne($new_url) {
if ($new_url != '') {
                $url_parts = explode('/', $new_url);
                $protocol = strtolower($url_parts[0]);
                if ($protocol == 'http:' || $protocol == 'https:' || $protocol == 'ftp:' || $protocol == 'feed:') {
                $protocol = "$protocol//";
                } else {
                $protocol = "";
                }
                $parsed_url = parse_url(trim($new_url));
                $parsed_url = trim($parsed_url[host] ? $parsed_url[host] : array_shift(explode('/', $parsed_url[path], 2)));
                $parsed_url = strtolower($parsed_url);
                $get_path_parse_url = parse_url($new_url, PHP_URL_PATH);                     
                $path_parts = explode("/",$get_path_parse_url);
                $extract_path_1 = $path_parts[1];
                $host_and_path_1 = "$protocol$parsed_url/$extract_path_1";
                
                return $host_and_path_1;

} else {
return "no url inserted";
}
}
?>

<?php
//usage
$url1 = "http://www.phpfreaks.com/forums/php-coding-help/?action=post";
$url2 = "www.phpfreaks.com/forums/php-coding-help/?action=post";
$url3 = "phpfreaks.com/forums/php-coding-help/";
$url4 = "HTTP://WWW.PHPFREAKS.COM/forums/php-coding-help/?action=post";

$example_1 = getPathOne($url1);
$example_2 = getPathOne($url2);
$example_3 = getPathOne($url3);
$example_4 = getPathOne($url4);

echo "$example_1<br />$example_2<br />$example_3<br />$example_4<br />";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.