1

I want to parse a URL and add each of its subpaths as an element in an array.

For example :

http://www.abcd.com/efgh/ijkl/mnop.php

for this URL the array will look like:

[
  'http://www.abcd.com',
  'http://www.abcd.com/efgh',
  'http://www.abcd.com/efgh/ijkl',
  'http://www.abcd.com/efgh/ijkl/mnop.php'
]

The problem is I am unable to add the exploded path to previous paths.

explode("/",parse_url($url, PHP_URL_PATH))
6
  • 1
    Did you try something so far? SO is not a place where people simple provide you with a solution! Commented Dec 21, 2016 at 17:50
  • i wan to help you, but i think @Vincent is right. it's not that tough. Commented Dec 21, 2016 at 17:54
  • i am not being able to explode parse_url($url, PHP_URL_PATH) and add it accordingly @bugscoder Commented Dec 21, 2016 at 17:56
  • 2
    Based on that last comment, it sounds like you do know how to do this. If you can show what you're doing currently that isn't working, I'm sure someone can explain why. Commented Dec 21, 2016 at 17:57
  • 1
    i will give you hint, you can try to explode the url and loop the array. Commented Dec 21, 2016 at 17:58

4 Answers 4

1

Here is one way to do it. You can break up the URL using parse_url. After exploding the path, I'm using array_filter to remove empties from the array, and array_values to reset the keys.

$url = 'http://www.abcd.com/efgh/ijkl/mnop.php';
$root = parse_url($url, PHP_URL_SCHEME) . '://' . parse_url($url, PHP_URL_HOST);
$paths = array_values(array_filter(explode('/', parse_url($url, PHP_URL_PATH))));

$arr[] = $root;
for ($i = 0; $i < count($paths); $i++) {
    $root .= '/' . $paths[$i];
    $arr[] = $root;
}
var_dump($arr);

Output:

array(4) {
  [0]=>
  string(19) "http://www.abcd.com"
  [1]=>
  string(24) "http://www.abcd.com/efgh"
  [2]=>
  string(29) "http://www.abcd.com/efgh/ijkl"
  [3]=>
  string(38) "http://www.abcd.com/efgh/ijkl/mnop.php"
}
Sign up to request clarification or add additional context in comments.

Comments

1

By parsing the url, and then exploding the paths you can build out each path by looping through the paths.

function urlArrays($url)
{
    $paths = array();
    $parsed = parse_url($url);
    if($parsed){
        $pathParts = explode("/",$parsed['path']);
        $current = $parsed['scheme'] . "://" . $parsed['host'];
        $paths[] = $current;
        for($i = 0; $i < count($pathParts); $i++)
        {
            if($pathParts[$i] != "")
            {
                $current = $current . "/" . $pathParts[$i];
                $paths[] = $current;
            }
        }
    }
    return $paths;
}

Comments

1

You can parse_url to build an array from the URL and explode function to split the path to an array and from there, you can loop through them and achieve what you're looking for. Here the code is:

function explode_url_path($url) {
    $url_parsed = parse_url($url);

    $url_scheme = $url_parsed['scheme'];
    $url_host = $url_parsed['host'];
    $url_path = $url_parsed['path'];

    $url_path_exploded = explode('/', ltrim($url_path, '/'));

    $url_array = array($url_scheme . "://" . $url_host);    
    $url_temp = $url_scheme . "://" . $url_host;

    foreach($url_path_exploded as $url_exploded) {
        $url_temp .= "/{$url_exploded}";
        $url_array[] = $url_temp;
    }

    return $url_array;
}

$url = 'http://www.abcd.com/efgh/ijkl/mnop.php';
$url_exploded = explode_url_path($url);

var_dump($url_exploded);

Comments

1

Here is an alternate method using string access and modification by character.

for ($i=0, $c=0, $path=''; isset($url[$i]); $path .= $url[$i++]) {
    if ($url[$i] == '/' && ++$c > 2) $paths[] = $path;
}
$paths[] = $url;

In this loop,

  • $i is the current position in the string
  • $c is a running total of / characters (to exclude the initial 2 after the scheme)
  • $path is the path as it gets rebuilt one character at a time

Basically you iterate over the string by character (or byte, really) and whenever you encounter a / after the initial two, you append the current path to the array. After the loop you append the entire URL to complete the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.