SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need URL PHP/Regex

    Imagine a website (a CMS) with the following sections...

    www.MySite.com/World
    www.MySite.com/Topics
    www.MySite.com/People

    In a file that's included in each section, I insert an echo value, similar to this:

    PHP Code:
    $MySection 'World';
    $MySection 'Topics';
    $MySection 'People'
    I'd like to simplify things by creating a single script that automatically names the sections for each website. It would be placed in a file that's included at the top of each section's static home page.

    So if someone types www.MySite.com/World or www.MySite.com/World/ (with a trailing slash) into their browser, the script would say, "Aha! The world that follows the first forward slash is World (or World/), therefore, $MySection must equal World, because we're omitting all trailing slashes."

    And if someone types www.MySite.com/World/Arizona, the script would say, "The first forward slash is followed by World/Arizona...but we can't have any forward slashes in $MySection, so we'll discard the second forward slash and everything that follows, leaving World as $MySection."

    Can anyone suggest a script that would do this?

    Thanks.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You may want to look at mod_rewrite if your running Apache, it can easily do this and more.

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    You may want to look at mod_rewrite if your running Apache, it can easily do this and more.

    I don't understand. I know mod_rewrite can be used to modify URL's, but can it examine segments of existing URL's and somehow translate them into PHP variables? I need to wind up with $MySection = 'World'; on a PHP file, and I don't understand how mod_rewrite could do that.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $s '/World/Arizona/';
    $s trim$s'/' );

    var_dumpsubstr$s0strpos$s'/' ) ) ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    mod_rewrite would rewrite http://www.yourserver.com/articles/ to http://www.yourserver.com/?section=articles.

    Which you could, as normal, obtain through the GET Super Global.

    $sSection = $_GET['section'];
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    An example of retrieving the first URL section, using a different approach than logic_earth, could look like:
    PHP Code:
    $path     '/World/Arizona';
    $section  current(explode('/'ltrim($path'/'), 2));  // Main line
    $sections = array('World''Topics''People');

    if ( ! 
    in_array($section$sections))
    {
        die(
    'Invalid section: ' $section);
    }

    echo 
    'Section: '$section
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I've almost got it.

    PHP Code:
    $path $_SERVER['REQUEST_URI'];
    $pathSection trim$path'/' );

    var_dumpsubstr$pathSection0strpos$pathSection'/' ) ) ); 
    When I type in MySite/Topics, it correctly echoes the section name (echo $pathSection). However, it displays it like this:

    Code:
    string(0) "" Topics
    How do I stop string(0) "" from displaying?

    Also, if I type in MySite/Topics/Horse, then $pathSection = Topics/Horse, rather than simply Topics.

    SilverBulletUK: OK, I understand what you're saying now. Unfortunately, I've already rewritten my URL's with mod_rewrite, and the format you suggested won't work for me. Thanks.

  8. #8
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    PHP Code:
    $path     '/World/Arizona';
    $section  current(explode('/'ltrim($path'/'), 2));  // Main line
    $sections = array('World''Topics''People');

    if ( ! 
    in_array($section$sections))
    {
        die(
    'Invalid section: ' $section);
    }

    echo 
    'Section: '$section
    You're a mind reader! I was just going to suggest a script that detected section names in the URL. Anyway, I tried out your script, and it works fine. Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •