|
Posted by dennis.sprengers on 02/23/07 00:11
Consider the following array and string:
$trail = array('products/veggies', 'products', 'services/cleaning');
$path = 'products/veggies/1243/more';
I am trying to write a function that matches $path with a value in
$trail. If no match is found, it should chop off "more" and compare
again. Then, chop off "1243" and compare again. Now a match is found:
"products/veggies". The function should return true.
Let's take another example: $path = 'services/industrial/gardening'.
There is no match for $path in $trail. Chopping off "gardening" leaves
"services/industrial". Again, no match. Chopping off "industrial"
leaves "services". No match will be found; the function should return
false.
This is what I have so far:
function in_trail($path) {
global $trail;
while ($path && !in_array($path, $trail)) {
$path = substr($path, 0, strrpos($path, '/'));
}
return $path ? true : false;
}
But this function always returns true! Could someone explain this to
me and tell me how to fix it? Your help is much appreciated :-)
Navigation:
[Reply to this message]
|