0

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

0

2 Answers 2

2

You should use parse_url instead:

$url = parse_url($_cat->getUrl());
$produrl = str_replace(".html","",$url['path']);
Sign up to request clarification or add additional context in comments.

5 Comments

I have updated my question I have 2 Cases sometimes I get website.com/category/subcategory.html and sometimes website.com/category.html does it still will work for it?
yes parse_url and the path part gives you everything after the domain name. php.net/manual/en/function.parse-url.php
Look at the link to the parse_url() function. $url in this case is the result array you get from parse_url. Everything that is after the domain name is stored with the key 'path'
wow it's such a great function! In sense of performance it's not heavy?
I personally have never benchmarked it but I highly doubt you will suffer any performance issues.
0

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

1 Comment

Yep I have checked that but I have no idea what method should I use to make it work and please note I have 2 cases Sometimes I get website.com/category/subcategory.html and sometimes website.com/category.html

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.