phptext-extractionurl-parsing

Get path from URL string


This is my code:

$produrl = '/'. basename(str_replace(".html","",$_cat->getUrl())) .'/' . basename($_product->getProductUrl()) ; 

$_cat->getUrl() returns this:

http://website.com/category/subcategory.html

I need: for $produrl part of basename(str_replace(".html","",$_cat->getUrl()))to return category/subcategory

The issue:

It gives back only category without /subcategory I know the issue is in the str_replace it's wrong isn't? Can you help me with that?

Pleast Note I have 2 Cases:

  1. Sometimes I get http://website.com/category/subcategory.html.

  2. Sometimes http://website.com/category.html

And I would like it to work with both of them :)


Solution

  • You should use parse_url instead:

    $url = parse_url($_cat->getUrl());
    $produrl = str_replace(".html","",$url['path']);