1

I have a string (url path) such as /recipes/cheese/cheese-meat-loaf and I have a giant array of paths such as

'/recipes/',
'/content/'
...

I want to take my path and find the path in the array that matches part of the path. So for this example it would match /recipes/ in the array as the path contains /recipes/, but the array may also contain /recipes/cheese/ in which case it should match that instead.

So pretty much I want to go through the path subtracting everything between the / and try and match it... So it would do

`/recipes/cheese/cheese-meat-loaf` then if not found
`/recipes/cheese/` then if not found
`/recipes/` then if not found
return default

and it would return the value in the giant array (the key is the path).

I want it to go backwards as I only want one value, and there may be multiple /recipes/*/

But I am unsure of how abouts to do this.

4
  • Sounds like a job for a recursive function Commented Oct 29, 2012 at 14:08
  • explode will probably help you. And yes you probably want to use recursion for simpler code. Commented Oct 29, 2012 at 14:09
  • @JohnConde that would be tail-recursive, so in a imperative programming language like php, I wouldn't recommend using recursion. Commented Oct 29, 2012 at 14:14
  • Loop and RegExp? Though what happens if you've got /recipes/cheese/cheese-meat-loaf and /recipes/cheese-meat-loaf - which would you want to return? Commented Oct 29, 2012 at 14:28

4 Answers 4

2

Since we're not here to do your coding for you, let me write up a "recipe" for you to write your code yourself. Then, if you come across a problem along the way, you can come back here with a much more specific question, which we can much better help you with.

  1. Search your array for the current search string
  2. If you found it, return the value in the array
  3. If you haven't found it, search your search string for the last occurrence of the slash character, and remove it along with everything following it
  4. Start again at 1 with your new search string
Sign up to request clarification or add additional context in comments.

Comments

0

There's a function in PHP called explode which expldes a string into pieces. explode("/",$inputstring) will return an array. For example:

explode("/",'/recipes/cheese/cheese-meat-loaf');

array(
    [0]=>"recipies"
    [1]=>"cheese"
    [2]=>"cheese-meat-loaf"
)

You can explode cheese-meat-loaf also.

Comments

0

Try to shorten your URL in a loop by cutting off the last / and search in your array with that value:

$arr = array([with many elements]);
$url = '/recipes/cheese/cheese-meat-loaf';
$found = false;
while ($found === false && $url !== '') {
  if (in_array($url, $arr)) $found = $url;
  else {
    $shorten = preg_replace('/(\/[^\/]*)$/', '', $url);
    if ($shorten === $url) break;
    else $url = $shorten;
  }
}
echo $found !== false ? 'found: ' . $url : 'not found';

Live example

Comments

0

Sort the array of paths so that the longest paths are first. Then just go through and return the first match:

$array = array(
    '/recipies',
    '/recipies/cheese'
    );

$url = "/recipies/cheese/fondue";

function lsort($a,$b){
    $diff = strlen($b) - strlen($a);
    return $diff;
}

usort($array,'lsort');

foreach( $array as $path ){
    echo "Testing $path<br/>";
    if( !strncmp($url, $path, strlen($path)) ){    
       echo "Best match is $path</br/>";
       break;
    }
}

Sample Output:

Testing /recipies/cheese
Best match is /recipies/cheese

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.