1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Request Uri Minus .php

Discussion in 'PHP' started by geekazoid, Jul 10, 2006.

  1. #1
    I would like to use this script

    <?php echo $_SERVER['REQUEST_URI']; ?>

    This script shows the url of the page that it is on

    For example Untitled4.php would be shown in the page

    Is there something i can do to the script to minus the .php bit so it just shows the Untitled4 ?
     
    geekazoid, Jul 10, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You might want to buy a PHP book :)

    PHP has excellent string manipulation functions.

    So...

    
    $full_uri = $_SERVER['REQUEST_URI'];
    $what_you_want = substr($full_uri, -4); //check this with php.net
    
    PHP:
     
    T0PS3O, Jul 10, 2006 IP
  3. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That looks perfect, what do i do with that ? and i may invest in a php book :D
     
    geekazoid, Jul 10, 2006 IP
  4. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What you do with it? I don't know, you asked for it! Use it for whatever you wanted to use it.

    BTW better would be, if there is only one dot:

    
    $uri_parts = explode(".", $full_uri);
    $what_you_want = $uri_parts[0];
    
    PHP:
     
    T0PS3O, Jul 10, 2006 IP
  5. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry i meant, where do i put it in the page. But i have sorted it now.
     
    geekazoid, Jul 10, 2006 IP
  6. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    OK i made a new page just to test that out this is my page

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script type="text/JavaScript">
    <!--
    function
    $uri_parts = explode(".", $full_URI);
    $what_you_want = $uri_parts[0]; 
    //-->
    </script>
    </head>
    
    <body><?php echo $_SERVER['REQUEST_URI']; ?>
    </body>
    </html>
    
    PHP:

    and the link to that page to view it is here

    Http://www.geekazoid.co.uk/Untitled-4.php

    As you can see the .php at the end is still there. Any Ideas ?
     
    geekazoid, Jul 10, 2006 IP
  7. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No offense but I think it's time for that book :)

    #
    <script type="text/JavaScript">
    #
    <!--
    #
    function
    #
    $uri_parts = explode(".", $full_URI);
    #
    $what_you_want = $uri_parts[0];
    #
    //-->
    #
    </script>
    Code (markup):
    That means nothing. What has JavaScript got to do with it? What is that 'function'?

    You used $full_URI in the explode funtion. But look at the lines preceding it... It never gets initiated. You forgot this line: $full_uri = $_SERVER['REQUEST_URI'];
     
    T0PS3O, Jul 10, 2006 IP
  8. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Im not affended, Im merely asking for help.

    So can you show me how i should use the script you have given
     
    geekazoid, Jul 10, 2006 IP
  9. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #9
    
    <?php
    $full_uri = $_SERVER['REQUEST_URI'];
    $uri_parts = explode(".", $full_uri);
    $what_you_want = $uri_parts[0];
    echo $what_you_want;
    ?>
    
    PHP:
    Expected result on http://www.geekazoid.co.uk/Untitled-4.php

    Doesn't work with folders...
     
    T0PS3O, Jul 10, 2006 IP
  10. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanx alot, And i will take you advice and purchase a book. Theres a lot i need to learn and i suppose i cant keep relying on the kind people here. Thanx
     
    geekazoid, Jul 10, 2006 IP
  11. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #11
    You might find members crazy enough to keep pasting you working code but it's not a viable solution for yourself long-term. If this stuff interests you, yeah I'd go get a book or do some tutorials online. If it's not something you fancy doing, then get an account a get-a-freelancer and build a relationship with a trusted coder.

    For everything in between, there's us geeks ready for you :)
     
    T0PS3O, Jul 10, 2006 IP
  12. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thanx alot its working now,

    http://www.geekazoid.co.uk/Untitled-4.php

    Im sorry to ask again but is there any way to get rid of the / in front of the url ? If this is a big job then dont worry i will go read up about it.

    Thanx for your help
     
    geekazoid, Jul 10, 2006 IP
  13. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #13
    mad4, Jul 10, 2006 IP
  14. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #14
    It's a massive job...

    
    $what_you_really_want = substr($what_you_want, 1);
    
    PHP:
    :)

    EDIT: I can see the next question coming so here's the answer:

    
    <?php
    $full_uri = $_SERVER['REQUEST_URI'];
    $uri_parts = explode(".", $full_uri);
    $what_you_want = $uri_parts[0];
    $what_you_really_want = substr($what_you_want, 1);
    echo $what_you_really_want;
    ?>
    
    PHP:
     
    T0PS3O, Jul 10, 2006 IP
  15. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Thankyou Very Much, The Script Is Working Perfectly

    ok i would love to be as in the know about php as you, Do you know of any good beginer tutorials ?
     
    geekazoid, Jul 10, 2006 IP
  16. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #16
    T0PS3O, Jul 10, 2006 IP
  17. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #17
    I learnt everything I know from google. Type what you want to do into the search box and 9 times out of 10 you get an answer. Also like T0PS says follow a few tutorials to get the hang of it. Most things in php have been done before so you may get lucky and just find something you can cut and paste.
     
    mad4, Jul 10, 2006 IP
  18. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #18
    I would suggest using dirname() and basename() respectively.

    basename() will give the name of the script but also has the benefit of being able to mask the extension given the second optional parameter. Here is a short example:

    
    <?php
    	/**
    	 * URL Parts Test Script
    	 * 
    	 * Short example created for geekazoid from DP forum
    	 *
    	 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
    	 * @version 1.0
    	 * @link http://www.oscommerce-freelancers.com/ osCommerce-Freelancers
    	 * @copyright Copyright 2006, Bobby Easland 
    	 * @author Bobby Easland 
    	 */
    
    	/**
    	 * Function to get the script name from path string
    	 * @author Bobby Easland 
    	 * @version 1.0
    	 * @param string $script
    	 * @param string $extension
    	 * @return string
    	 */	
    	function getScriptNameMinusExtension($script, $extension = '.php'){
    		return basename($script, $extension);
    	} # end function
    	
    	/**
    	 * Function to get the directory name from path string
    	 * @author Bobby Easland 
    	 * @version 1.0
    	 * @param string $path
    	 * @return string
    	 */	
    	function getDirName($path){
    		return dirname($path);
    	} # end function
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>URL Parts Test Script</title>
    </head>
    <body>
    	<!-- Output the directory name //-->
    	<p>Directory Name: <?php echo getDirName($_SERVER['SCRIPT_NAME']); ?></p>
    	<!-- Output the script name //-->
    	<p>Script Name: <?php echo getScriptNameMinusExtension($_SERVER['SCRIPT_NAME'], ''); ?></p>
    	<!-- Output the script name minus the PHP extension //-->
    	<p>Script Name (minus extension): <?php echo getScriptNameMinusExtension($_SERVER['SCRIPT_NAME']); ?></p>
    </body>
    </html>
    
    PHP:
    Bobby
     
    Chemo, Jul 11, 2006 IP
    T0PS3O likes this.