phpmady Posted January 1, 2011 Share Posted January 1, 2011 Hi guys, Happy New Year http://www.phpfreaks.com/forums/php-coding-help/?action=post How can i get the path http://www.phpfreaks.com/forums thanks, Link to comment https://forums.phpfreaks.com/topic/223120-getting-the-path/ Share on other sites More sharing options...
QuickOldCar Posted January 1, 2011 Share Posted January 1, 2011 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 https://forums.phpfreaks.com/topic/223120-getting-the-path/#findComment-1153563 Share on other sites More sharing options...
QuickOldCar Posted January 1, 2011 Share Posted January 1, 2011 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 https://forums.phpfreaks.com/topic/223120-getting-the-path/#findComment-1153594 Share on other sites More sharing options...
phpmady Posted January 1, 2011 Author Share Posted January 1, 2011 thank you very much friend, Its useful to me Link to comment https://forums.phpfreaks.com/topic/223120-getting-the-path/#findComment-1153673 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.