Tag for a tag page

Discussion in 'PHP' started by goodmast3r, Jan 2, 2010.

  1. #1
    goodmast3r, Jan 2, 2010 IP
  2. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here is how the concept of that works:
    <?php
    
    $url = 'http://www.google.com/path/test/two';
    
    # /path/test/two
    $parsed = parse_url($url, PHP_URL_PATH);
    
    # path/test/two
    $fix = substr($parsed, 1, strlen($parsed) -1);
    
    
    $parts = explode('/', $fix);
    /*
    Array
    (
        [0] => path
        [1] => test
        [2] => two
    )
    */
    PHP:
    And this next one is a simplified function with comments.
    <?php
    /***********************
    * URI Segment
    ************************
    * Written by Joel Larson (Coded Caffeine)
    *   Author URL: http://thejoellarson.com/
    *   Date: 26.12.09
    ***********************/
    
    /**
     * uri_segment()
     *
     * Parses the URI segments from a URL.
     *
     * @access	public
     * @param	int
     * @param	string
     * @return	string
     */
    function uri_segment($id = 0, $segment = NULL)
    {
    	# Parse the segments into one/two/three
    	$path = substr(parse_url($segment, PHP_URL_PATH), 1, strlen($segment)-1);
    	
    	# Explode the forward slash, making a uri segment array.
    	$parts = explode('/', $path);
    	
    	# Return the specified array ID. (Starts at 0)
    	return $parts[$id];
    }
    PHP:
    This is how it's called:

    <?php
    #URL
    $url = 'http://example.com/segment/one/two/three';
    
    # Returns 'one'
    $segment = uri_segment(1, $url);
    
    
    PHP:
     
    Last edited: Jan 2, 2010
    CodedCaffeine, Jan 2, 2010 IP
  3. goodmast3r

    goodmast3r Active Member

    Messages:
    1,220
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    78
    #3
    That will work, but do you know how to find the tag at wordpress? Sorry, I guess I didn't give clear description.
     
    goodmast3r, Jan 2, 2010 IP
  4. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you need this in your template files, it's possible to add the function to function.php and use it.

    To my knowledge, there isn't a pre-existing function for URI segments.
     
    CodedCaffeine, Jan 3, 2010 IP