Hi,
If I have the following url:
http://URL/products/38/293/bannana_cake/
or
htp://URL/products/38/293/fruit_cake/
How can I isolate just bannana_cake and fruit_cake from the examples above?
Hi,
If I have the following url:
http://URL/products/38/293/bannana_cake/
or
htp://URL/products/38/293/fruit_cake/
How can I isolate just bannana_cake and fruit_cake from the examples above?
it will be a $_GET variable as what you're looking at is just a mod_rewrite version of a query string
try this to see what the variable name is:
<pre>
<?php print_r($_GET);?>
</pre>
<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
And then use explode() on the 'path' element.
For example:
<?php
$url = 'http://URL/products/38/293/bannana%5Fcake/';
$a = parse_url($url);
$p = explode('/', $a['path']);
echo $p[4];
?>
You can see the URL that has been requested by looking at the $_SERVER[] array (Google that to find the exact entry). They you can split the string into an array on '/', then the [3] index will be the part of the URL you're interested in.
Split the url on the slashes and retrieve the last part:
$parts = explode('/', $url);
echo $parts[sizeof($parts) - 2];
Only problem, you need to have the trailing slash in the url. You could make a check for that like this:
$parts = explode('/', $url);
echo ($parts[sizeof($parts) - 1])
? $parts[sizeof($parts) - 1]
: $parts[sizeof($parts) - 2];
Probably the easiest way, which only applies to your special case and is not something for production, is to use the basename-function:
<?php
echo basename("http://url/products/38/293/banana_cake/"); // Produces "banana_cake"
?>
This only works because "banana_cake" is the last part of the url and there is nothing behind the last slash.
It is definately not a desirable solution and Luca Matteis' answer will get my vote, because the slightest change in the query string order will break things.
My answer will be slightly longer. It looks like you want to do something similar to using URI Templates, so here's a snippet of two functions from a class (called xs_Breakdown) I have that does these things. It could easily be extended to include wildcards and conditional behaviour (on the todo list for a time in the future I'm suffering from too little to do). First, and example of setting up and use ;
$br = new xs_Breakdown ( '{first}/{second}/{third}/{fourth}/{fifth}/{andsoon}' ) ;
// Pick out the template variable called 'third'
$third = $br->third ;
The code (just the basics which should be enough to kick up some of your own dust; all the code would be too long to post here. Pop me a message if you'd like the whole shebang with three nested property / Java Bean-like classes) ;
// Static variable to hold our tokens
$_tokens = null ;
// Input path (set in constructor)
$_path = null ;
// Results here
$values = array() ;
function parse ( $schema = '' ) {
// Sanitize input data : Regular Expression
$regexp = '/[^a-z0-9 +\-\/!$*_=|.:]/i' ;
// Break our path into little bits
$break = explode ( '/', $this->_path ) ;
// Find the tokens used from our schema template
$this->_tokens = $this->getSubStrs ( "{","}", $schema ) ;
// Loop through the path elements
foreach ( $break as $key => $value ) {
// Sanitize the value of the element
$value = urldecode ( trim ( preg_replace ( $regexp, '', $value ) ) ) ;
// Element not blank? (Meaning, real text)
if ( $value != '' )
// Index it!
@$this->values[$this->_tokens[$key]] = $value ;
}
}
function getSubStrs ( $from, $to, $str, &$result = array () ) {
if ( strpos ( $str, $from ) !== false ) {
$start = strpos ( $str, $from ) + 1 ;
$end = strpos ( $str, $to ) - 1 ;
$item = substr ( $str, $start, $end - $start + 1 ) ;
$rest = substr ( $str, $end + 2 ) ;
$result[] = $item ;
$this->getSubStrs ( $from, $to, $rest, $result ) ;
}
return $result ;
}