# Get any segment of current url

**URL:** <https://forum.getkirby.com/t/get-any-segment-of-current-url/29057>\
**Category:** Questions\
**Tags:** v3\
**Created:** [July 11, 2023, 10:13am UTC](https://forum.getkirby.com/t/get-any-segment-of-current-url/29057 "2023-07-11T10:13:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![coko](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@coko](https://forum.getkirby.com/u/coko)\
**Post date:** [July 11, 2023, 10:13am UTC](https://forum.getkirby.com/t/get-any-segment-of-current-url/29057/1 "2023-07-11T10:13:04Z")

</div>

Title: How would you get any segment of the current url?

I’ve been using something like this:

```auto
$segments = explode('/', $page->uri());
$target = $segments[0] . '/' . $segments[1];

```

Any alternative solution you like to share?  
Thanks

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 11, 2023, 10:48am UTC](https://forum.getkirby.com/t/get-any-segment-of-current-url/29057/2 "2023-07-11T10:48:04Z")

</div>

You can use the [URI](https://getkirby.com/docs/reference/objects/http/uri) class:

```auto
dump((new Kirby\Http\Uri($page->url()))->toArray());

```

---

<div class="post-metadata">

**Author:** ![coko](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@coko](https://forum.getkirby.com/u/coko)\
**Post date:** [July 11, 2023, 11:14am UTC](https://forum.getkirby.com/t/get-any-segment-of-current-url/29057/3 "2023-07-11T11:14:26Z")

</div>

How do I get a value from the path array?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 11, 2023, 11:15am UTC](https://forum.getkirby.com/t/get-any-segment-of-current-url/29057/4 "2023-07-11T11:15:54Z")

</div>

Have you inspected the result? Do you know how to get indices from an array?

This is an example result:

```auto
Array
(
    [fragment] => 
    [host] => kirby.test
    [password] => 
    [params] => Array
        (
        )

    [path] => Array
        (
            [0] => notes
            [1] => exploring-the-universe
        )

    [port] => 
    [query] => Array
        (
        )

    [scheme] => http
    [slash] => 
    [username] => 
)

```

---

<div class="post-metadata">

**Author:** ![coko](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@coko](https://forum.getkirby.com/u/coko)\
**Post date:** [July 11, 2023, 11:21am UTC](https://forum.getkirby.com/t/get-any-segment-of-current-url/29057/5 "2023-07-11T11:21:47Z")

</div>

Yes, I have looked at the result but I got an Object.

This seems to work for me:

```auto
  ($uri = new Kirby\Http\Uri($page->url()));
  dump($uri->toArray()); // gives an Array like your example
  dump($uri->path->data()[2]) // displays the third segment

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 11, 2023, 11:35am UTC](https://forum.getkirby.com/t/get-any-segment-of-current-url/29057/6 "2023-07-11T11:35:49Z")

</div>

> [@coko](#):
>
> `->toArray()`

When calling a method directly on the newly instantiated object (instead of on two lines like you are doing it now), you have to wrap the `new Something()` in parentheses, see my code example above.

So

```auto
$uriArray = (new Kirby\Http\Uri($page->url()))->toArray();

```

Whereas here

> [@coko](#):
>
> ` ($uri = new Kirby\Http\Uri($page->url()));`

the outer parentheses are superfluous.

---

<div class="post-metadata">

**Author:** ![coko](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@coko](https://forum.getkirby.com/u/coko)\
**Post date:** [July 11, 2023, 12:53pm UTC](https://forum.getkirby.com/t/get-any-segment-of-current-url/29057/7 "2023-07-11T12:53:33Z")

</div>

Ok, thank you

Just to be complete and as a reference:

```auto
$uri = (new Kirby\Http\Uri($page->url()))->toArray();
dump($uri) // get an overview of the Array
echo $uri['path'][1] // output the second segment

```
