1

So i wrote some code for making seo-friendly urls. These functions first make a seo-friendly slug and then if the slug already exists is the DB(in this case array) then they add a number with a dash next to it. if that also exists then they just +1 the number and then checks again and again...

eg. if i pass "title url" to the function. First it will convert it to "title-url" and if "title-url" already exists then it will add a number like "title-url-1" if it exists as well then it will +1 the number like "title-url-2" and then "title-url-3" and so on...

this is the code:

// CONVERTS STRING TO URL SLUG
function str_to_slug($str){
    $str = strtolower(trim($str));
    $str = preg_replace('/[^a-z0-9-]/', '-', $str);
    $str = preg_replace('/-+/', "-", $str);
    return $str;
}

// RETURN SLUG URL
function slug($title){
    $ori_url = str_to_slug($title);
    if( does_slug_exists($ori_url) ){ 
       return loop_slug_number($ori_url, 1); 
    }
    else{ 
       return $ori_url; 
    }
}

// ADD NUMBER
function loop_slug_number($slug, $number){
    if( does_slug_exists($slug.'-'.$number) ){ 
        loop_slug_number($slug, $number++); 
        exit; 
    }
    else{ 
        return $slug.'-'.$number; 
    }
}

// CHECKS WHEATHER THE SLUG EXISTS IN THE DB
function does_slug_exists($slug){
    $array = array("title", "title-0", "title-1", "title-2");
    return (in_array($slug, $array)) ? true : false;
}

i think everything should work fine. but when i echo slug("title"); i'm getting

Fatal error: Maximum function nesting level of '100' reached, aborting!

error line number is in the function does_slug_exists() on the 'return' line.

(the array is just for example i will use db validation.)

also if i replace the array with:

$array = array("title", "title-0", "title-2", "title-3");

then i get title-1 back.

Where is the mistake?

3
  • Why do you need a recursive call for loop_slug_number? You can do it with a "while" loop just as well. Commented Feb 6, 2012 at 10:49
  • yes. but what is the problem hear? Commented Feb 6, 2012 at 10:55
  • 1
    the problem is that the "number" doesn't get incremented until after the loop_slug_number gets called. Therefore, loop_slug_number is called always with "number = 1" Commented Feb 6, 2012 at 11:20

3 Answers 3

1

Ignoring any comments about the code quality, the issue here is the post-increment of the $number variable. You can replace with:

return loop_slug_number($slug, ++$number);

However, I suggest that the entire function should be rewritten as a while loop as opposed to a pseudo-recursive function. In addition, it looks like a DB query is made upon each call of does_slug_exists(); I suggest you refactor this to make the query once and store the returned result set. Have a look at this example.

Sign up to request clarification or add additional context in comments.

Comments

0
// ADD NUMBER
function loop_slug_number($slug, $number){
    if( does_slug_exists($slug.'-'.$number) ){ loop_slug_number($slug, $number++); exit;     }else{ return $slug.'-'.$number; }
}

This is really awful code. Instead of looping, use a while loop. Start the number at 0 and while the slug exists, increment the number.

Comments

0

I'm not sure about PHP, but in C you should do a ++number instead. The idea is that the number gets incremented after the function was called if you do a number++ and before if you do ++number.

.. the joys of increment/decrement operators...

1 Comment

You could do a print to see the number value in loop_slug_number.

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.