Board index   FAQ   Search  
Register  Login
Board index PHP Your Scripts

Simple URI Parser

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

Simple URI Parser

Postby Xenres » Tue Mar 16, 2010 5:16 pm

Well, I just got here, so I figured I'd contribute something. I've posted this elsewhere, so you will find it in other places, but I figure this is as good as any place to re-post it. Also, I've added lots of extra comments so people will understand what's going on.

Code: Select all
// Set our variables for self ( $self ), and our install uri ( $iuri )
$self   = pathinfo(__FILE__, PATHINFO_BASENAME);
$iuri   = str_replace($self, '', $_SERVER['SCRIPT_NAME']);    // we strip out $self
                                               // so we don't parse
                                               // it twice if you
                                               // use the .htaccess

// OK, now I'm going to make them constants so that I can use them in my nifty
// function which can be placed in any script and not be stuck within the
// environment config script if you don't want it there.

define('SELF', $self);
define('IURI', $iuri);

// Ok, now here's my workhorse function, just feed it the number for the segment
// that you want and it will output it's value.

function seg($n)
{
   // off by one fix
   $n   = $n - 1; // this way we can count from 1 instead of 0
   
   // get our uri
   $ruri   = $_SERVER['REQUEST_URI'];
   
   // explode our install uri into an array
   $iuri   = explode('/', IURI);
   
   // now lets do some filtering, we'll be using preg_replace so that we will
   // only using the first occurance of any terms that may appear in our SELF
   // or IURI
   $ruri   = preg_replace('/'.SELF.'/', '', $ruri, 1);
   
   // now we are going to take our $iuri array and filter out the individual
   // components
   foreach($iuri as $k => $v)
   {
      // you will note that I don't use the key, because I don't really care
      // what the key ($k) is, it's not relevant to the stripping process, and only
      // helps with the iteration of the array values ($v)
      
      $ruri   = preg_replace('/'.$v.'/', '', $ruri, 1);
   }
   
   // now to create our segment array
   // this line is read in reverse, first we explode the array with explode()
   // then we get rid of any empty values with array_filter()
   // and finally we reset the array index with array_merge() by merging
   // our new array with an empty array.
   $seg    = array_merge(array(), array_filter(explode('/', $ruri)));
   
   // if the segment requested is not set, let's return false.
   if( ! isset($seg[$n])) return FALSE;
   
   // if the segment starts with a ? then it's a $_GET value and not an uri
   // value, so we don't want to parse it.
   $value   = $seg[$n]
   if( $value[0] == "?") return FALSE;   // [0] will grab the first character of
                              // the variable
   
   // ok, now that we've determined everything is kosher, and that our value
   // not only exists, but is a valid value, let's filter it, and return it.
   $value   = htmlspecialchars($seg[$n], ENT_QUOTES);
   
   return $value;
}


simple, right?

OK, now if you are using Apache, here's a simple method to remove the index.php from your URI. I've got it so that it will either use mod_rewrite, or if mod_rewrite is unavailable, it'll use a dirtier method using the 404 redirect. Just put it into a file at the root of your sites install path, and save it as .htaccess. ( This is for Apache, other server software may not be capable of this, or may require a different method. In fact, if they are capable, they will most certainly require a different method )
Code: Select all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>


OK, well that's pretty much it in a nutshell, hope it helps someone out in their next web project.
Xenres
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Mar 16, 2010 4:33 pm

Return to Your Scripts

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.