str_replace的小问题

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 :)

You should use parse_url instead:

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

The problem is that you're using the basename() function.

Take a look at the documentation for it: http://php.net/manual/en/function.basename.php

It will only return the trailing string after the "/" (forward slash) or "\" (back slash, for Windows).