Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5942905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:19:09+00:00 2026-05-22T16:19:09+00:00

I am currently using the following code to achieve a page, section and class

  • 0

I am currently using the following code to achieve a page, section and class variable from the url.

$domain = 'http://' . $_SERVER['HTTP_HOST'];
$path = $_SERVER['REQUEST_URI'];
$url = $domain . $path;

// page + section + class
$page = basename($url);
$page = $class = str_replace('.php','',$page);
$page = str_replace('-',' ',$page);

if ($path == "/") {
    $section = $class = "home";
} else if (basename(dirname($url),"/") == $_SERVER['HTTP_HOST']) {
    $section = $page;
} else { 
    $section = basename(dirname($url),"/");
    $section = str_replace('-',' ',$section);
    $class =  basename(dirname($url),"/") . " " . $class;
}

For example if the url is http://www.mydomain.co.uk/about/ the code will return the following variables:

$page = "about"
$section = "about"
$class = "about"

For http://www.mydomain.co.uk/about/general-info/

$page = "general info"
$section = "about"
$class = "about general-info"

But when I add more depth for example For http://www.mydomain.co.uk/about/general-info/history/ to code produces:

$page = "history"
$section = "general info"
$class = "general-info history"

where ideally I need it to output the following:

$page = "history"
$section = "about general info"
$class = "about general-info history"

or breakdown the sections into as many as needed for example:

$section1 = "about"
$section2 = "general-info"

Hopefully someone can help. If anything is unclear please ask.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:19:10+00:00Added an answer on May 22, 2026 at 4:19 pm

    So you want an URI-scheme as follows?

    http://<domain>/<section-1>/<section-2>/.../<page>/
    

    Special cases:

    (1) http://<domain>/            -- page is empty, section is "home"
    (2) http://<domain>/<section>/  -- page and section are '<section>'
    

    Instead of relying on basename(), you should split your string into an array using explode() or preg_split(). You can use REQUEST_URI directly since the domain name does not give any extra information for your sections and page.

    Once you have an array, you can easily count() the number of path components, handle special cases for empty and size-one paths, and so on. In the following example, I use array_pop() to extract the last part of the path to separate the page from the sections. Since you seem to desire space-separated strings for sections and page, I use implode() to join the arrays back into a string.

    // No need for the domain stuff!
    $path = $_SERVER['REQUEST_URI'];
    
    // Split at '/', could use explode() but the PREG_SPLIT_NO_EMPTY flag is
    // very handy since it handles "//" and "/" at start/end.
    $tokens = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
    
    if (count($tokens) == 0) {
        // Special case 1
        $page = "";
        $section = $class = 'home';
    } elseif (count($tokens) == 1) {
        // Specical case 2
        $page = $section = $class = $tokens[0];
    } else {
        // Class contains all tokens.
        $class = implode(' ', $tokens);
    
        // The last part is the page.
        $page = array_pop($tokens);
    
        // Everything else are sections.
        $sections = implode(' ', $tokens);
    }
    
    // You seem to want spaces for dashes in the section:
    $section = str_replace('-', ' ', $section);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In short, currently I am using the following code to pull records from multiple
I'm currently downloading an HTML page, using the following code: Try Dim req As
I am currently using the following code to create a web request: Dim myRequest
I'm currently using the following code to append a X link to each element
I'm currently using the following code (C#): private static void PlayLoop(string filename) { Audio
I'm currently using StructureMap to inject instances of NHibernate ISessions using the following code:
I'm currently using the following PHP code: // Get all subordinates $subords = array();
I am using currently the following code to populate a combobox: combobox.DataSource = datatable;
I am currently using PHP_Beautifier for formatting code with the following command like options
I'm currently using the following code to output text in green to the terminal:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.