Miomir Dancevic
Miomir Dancevic

Reputation: 6852

Creating array from string php (url)

I have some link, lest say string

$string = "http://www.example.com/proizvodi/pokloni/kuhinja/?page=1";

I need new array $links

That will look like this

$links =array('proizvodi/','proizvodi/pokloni/', 'proizvodi/pokloni/kuhinja/');

I have tried

$crumbs = explode("/",$string]);

But the problem is that i dont need ?page=1 and first

Any idea, will be nice, txanks

Upvotes: 0

Views: 41

Answers (1)

mehulmpt
mehulmpt

Reputation: 16577

If you're looking for just the breadcrumbs in path, do something like this:

$url = "http://www.example.com/proizvodi/pokloni/kuhinja/?page=1";

$decomposedURL = parse_url($url);
$crumbs = array_filter(explode("/", $decomposedURL['path'])); // array_filter to remove empty elements

UPDATE: What you want now is all the crumbs stacked over each other. Again, you can do that by the following code:

$links = [];
$lastString = "";
for($i=0;$i<count($crumbs);$i++) {
  $lastString += $crumbs[i]+"/";
  $links[] = $lastString;
}

Upvotes: 3

Related Questions