Forum

Thread tagged as: Problem

How to get the URL to a page w/o the page itself

I would like to know the path to a page without the page itself. So for

`/the-boat/history.php'

I would like

`/the-boat/'

returned

the same returned for

`/the-boat/index.php'

and for

`/the-boat/leaking.php'

but for

`/the-docks/history.php'

I would get

`/the-docks/'

Sorry that's a longwinded explanation. I've looked at https://docs.grabaperch.com/docs/navigation/perch-page-url but it doesn't quite give me what I want. Is there some other Perch construct I can use to return this (there does not seem to be consensus on a reliable way to do it in raw PHP)?

Thanks in advance for any pointers ^_^

Alan Bristow

Alan Bristow 0 points

  • 6 years ago
Simon Clay

Simon Clay 127 points

Hi Alan,

I think you should be able to do it with this:

<?php 
    $Perch = Perch::fetch();
    $filePath = $Perch->get_page(); //FIND THE FILE PATH
    $fileName = basename($filePath); //FIND THE FILE NAME
    $pathOnly = str_replace($fileName, '', $filePath); //SET $pathOnly TO BE THE FILEPATH WITHOUT THE FILENAME

    echo($pathOnly); // $pathOnly NOW AVAILABLE FOR USE :)
?>

Thanks Simon, works a treat! Most grateful ^_^

Worked for me also, thanks Simon.

Simon Clay

Simon Clay 127 points

Great! :)