user3571494
user3571494

Reputation: 167

How to get first 3 parts of URL in PHP?

How to get first 3 parts of current URL by using PHP.

For example:

My Url: http://something.com/somebody/somegirls/whatever/

The result after getting parts: http://something.com/somebody/somegirls/

This is my code PHP which get current URL:

<?php   function curPageURL() {
        $url  = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http';
        $url .= '://' . $_SERVER['SERVER_NAME'];
        $url .= in_array( $_SERVER['SERVER_PORT'], array('80', '443') ) ? '' : ':' . $_SERVER['SERVER_PORT'];
        $url .= $_SERVER['REQUEST_URI'];
        return $url;
    }

 $current_url = str_replace("www.", "", curPageURL());

?>

Upvotes: 6

Views: 2988

Answers (7)

Himansu
Himansu

Reputation: 21

Please check the below code.

function createUrl($array, $pos) {
    $string = '';
    for ($i = 0; $i < $pos; $i++)
    $string .=$array[$i].'/';
    return $string;
}

$current_url = "http://something.com/somebody/somegirls/xyz/yui";
$initial_string = (stripos($current_url, 'https://') !== FALSE) 
                       ? 'https://'
                       : ((strpos($a, 'http://') !== FALSE)
                       ? 'http://' : '');
$last_string = explode('/', substr($a, strlen($initial_string)));

$final_url = $initial_string.
        (count($last_string) > 3)
        ? createUrl($last_string, 3)
        : substr($current_url, strlen($initial_string));
echo $final_url;

Upvotes: 1

hizbul25
hizbul25

Reputation: 3849

Try with this :

<?php
$url = 'http://something.com/somebody/somegirls/whatever/';
$pos = explode('/', $url);
for($i=0; $i<5; $i++){
    echo $pos[$i].'/';
}
?>

Output: http://something.com/somebody/somegirls/

Upvotes: 1

Assuming that you have grabbed this URL from your function...

<?php
$url='http://www.something.com/somebody/somegirls/whatever/';
$parts=explode('/',parse_url($url)['path']);
array_unshift($parts,trim(strstr(parse_url($url)['host'],'.'),'.'));
print_r(array_filter($parts));

OUTPUT :

Array
(
    [0] => something.com
    [2] => somebody
    [3] => somegirls
    [4] => whatever
)

Demonstration

Upvotes: 5

Bijay Rai
Bijay Rai

Reputation: 959

<?php
   $url='http://something.com/somebody/somegirls/whatever' ;
    $explode=explode("/", $url);
    $search=end($explode);

   echo $currentUrl=str_replace($search,'',$url);
?>

Output

http://something.com/somebody/somegirls/

Upvotes: 1

Guillaume
Guillaume

Reputation: 29

You can use regexp:

<?php

    function getFirstUrlContents($Url) {
        preg_match_all('/^([^\/]*\/){5}/', $Url, $MatchesArray);
        return $MatchesArray[0];
    }

    var_dump(getFirstUrlContents('http://something.com/somebody/somegirls/whatever/'));

?>

Upvotes: 1

Nadeem Khan
Nadeem Khan

Reputation: 3424

You can also use parse_url to get the url in parts in an array like this:

$current_url_array = parse_url($current_url);
var_dump($current_url_array);

Upvotes: 2

Vinod VT
Vinod VT

Reputation: 7149

Try this,

<?php
  $url = 'http://something.com/somebody/somegirls/whatever/';
  $parts = explode('/', $url);
  $new_url = $parts[0].'/'.$parts[1].'/'.$parts[2].'/'.$parts[3].'/'.$parts[4].'/';
  echo $new_url;
?>

OUTPUT

http://something.com/somebody/somegirls/

Upvotes: 8

Related Questions