How to get first 3 parts of URL in PHP?

php, url

Solution

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/

Problem

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()); ?> ```

Original source