|
Posted by Steve on 02/23/07 15:19
<dennis.sprengers@gmail.com> wrote in message
news:1172229933.456121.312580@q2g2000cwa.googlegroups.com...
| Thanks, your function works great. However, it boggled my mind why it
| didn't do the job in my website, then. After hacking away for like an
| hour, it occured to me: the function should do more than just chopping
| and comparing. Please consider:
|
| $trail = array('products/veggies/winter', 'products/veggies',
| 'products');
|
| $path_1 = 'products'; // true
| $path_2 = 'products/423'; // false
| $path_3 = 'products/veggies'; // true
| $path_4 = 'products/meat'; // false
| $path_5 = 'products/veggies/23'; // false
| $path_6 = 'products/veggies/winter'; // true
| $path_7 = 'products/veggies/winter/23/edit'; // true
| $path_8 = 'products/meat/cow/dried/54/edit'; // false
|
| The longest element in $trail is 'products/veggies/winter'. It has 3
| parts (products, veggies and winter). I will call this element "A".
|
| The function compares $path with $trail. if $path consists of 3 parts
| or less, try finding a direct match. If there is a direct match,
| return true. This is the case with $path_1, $path_3 and $path_6.
| $path_2, $path_4 and $path_5 also have 3 or less parts, and there is
| no direct match with $trail, so we return false.
|
| If $path has more element-parts than A (in this case: 3), chop off the
| extra parts and look if we now have a match. Two examples are $path_7
| and $path_8:
|
| - $path_7 has 5 parts, which is 2 more than A. We chop off "23/edit"
| and compare "products/veggies/winter" to A, which will match and thus
| return true
| - $path_8 has 6 parts, which is 3 more than A. We chop off "dried/54/
| edit" and compare "products/meat/cow" to A, which will return false
|
| Could you please help me putting this into a function? I have no
| trouble describing what should happen, but find it difficult
| translating it into an efficient function.
|
| Thanks in advance for any reply :-)
so what you're saying is that if, from left to right, path is equal to trail
(where path or trail is partially compared), then true else false? in that
case, the fix is easy:
$pathLength = strlen($path);
foreach ($trail as $comparison)
{
$length = min($pathLength, strlen($comparison));
if ($length < 1){ continue; }
if (substr($path, 0, $length) == substr($comparison, $length)){ return
true; }
}
return false;
this assumes, by your description of course, that we don't care to which
trail path most closely resembles...we just want at least either the full
path to meet a partial trail or, a partial trail to equal a full path. this
is the gist, however you do want to put in one other validation - to make
sure $path and $comparison are a full directory name... such that:
$path = 'j';
$trail = 'jennifer/is/a/hottie';
won't return true...which it should based on the code example i just gave.
hth
[Back to original message]
|