Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Function to convert MYSQL results to SEO-friendly URL's

    Hi all,

    I'm just learning php and I've created a php function to clean up the results of a mysql query and convert the result to an SEO-friendly url. This function works fine but is probably a lot more clumsy and cumbersome than it needs to be and I am hoping someone can help me clean it up a little.

    The function can take titles like these:

    Los Angeles, CA 8/9/09
    Who am I?
    (Just) Crazy

    and turn them into url's like these:

    los-angeles-ca-8-9-09
    who-am-i
    just-crazy

    PHP Code:
    <?php
    // this function removes all non-alphanumeric characters from a url, adds hyphens between words, makes all letters lowercase
    function cleanurl($string)
    {
    // replaces anything not a letter, number, or a space with one white space
    $cleanurl preg_replace('/[^A-Za-z0-9\s]/i'' '$string);
    // eliminates extra white spaces created above
    $cleanurl preg_replace('/\s\s+/'' '$cleanurl);
    // replaces white space with a hyphen
    $cleanurl str_replace(' ''-'$cleanurl);
    // removes any hyphen from beginning of string
    $cleanurl preg_replace('/^-/'''$cleanurl);
    // removes any hyphen from end of string
    $cleanurl preg_replace('/-$/'''$cleanurl);
    // makes all letters lower case
    $cleanurl strtolower($cleanurl);
    // returns the clean url to the page
    return $cleanurl;
    }
    ?>
    As you can see, it's taking me many separate progressions to get the desired result. There must be a more straightforward and less resource-intensive way to accomplish this.

    Thanks,
    Keith
    Last edited by squidnunc; 09-14-2009 at 09:13 PM.

  • #2
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    Here's an example from the PHP documentation:

    PHP Code:
    <?php
    $string 
    'The quick brown fox jumped over the lazy dog.';
    $patterns[0] = '/quick/';
    $patterns[1] = '/brown/';
    $patterns[2] = '/fox/';
    $replacements[2] = 'bear';
    $replacements[1] = 'black';
    $replacements[0] = 'slow';
    echo 
    preg_replace($patterns$replacements$string);
    ?>
    You can use arrays as your arguments to preg_replace to simplify it a little bit.

  • #3
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you very much for responding, bacterozoid. Would the example you suggested using just pretty-up the code or actually make the function less resource-intensive?

    Also, the function I wrote had to work in the order it was written or it would not perform the same. Would the example you suggested maintain the proper progression of replaces?

  • #4
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    squid,

    It should speed up the code, but I doubt you will notice. You're calling preg_replace fewer times, is all. It still has to do all the replacement work.

    It will also work in the proper order, so long as you fill your arrays in the same order. $array[0] should be the first replacement, $array[1] the second, and so on.

  • #5
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Based on the example you provided, I rewrote the function as follows:

    PHP Code:
    <?php
    // this function removes all non-alphanumeric characters from a url, adds hyphens between words, makes all letters lowercase
    function cleanurl($string)
    {
    $patterns[0] = '/[^A-Za-z0-9\s]/';
    $patterns[1] = '/\s\s+/';
    $patterns[2] = '/\s/';
    $patterns[3] = '/^-/';
    $patterns[4] = '/-$/';
    $replacements[0] = ' ';
    $replacements[1] = ' ';
    $replacements[2] = '-';
    $replacements[3] = '';
    $replacements[4] = '';
    $cleanurl preg_replace($patterns$replacements$string);
    $cleanurl strtolower($cleanurl);
    return 
    $cleanurl;
    }
    ?>
    The function as rewritten still works fine and it seems to be more elegant code. This is the first php function I have written. Does this seem to be the best way to accomplish my goal and would you consider this to be a useful function? Thanks for all your help, bacterozoid!

  • #6
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    It looks pretty good to me. The only thing I would change is to do strtolower() first, then you can change your first regular expression and remove uppercase letters.

    If you prefer, you could also initialize your arrays like this:

    PHP Code:
    $replacements = Array('value''value''value'); 
    It just makes your code look different. In this case, I would do that because it's my personal preference to squeeze more crap on the same line (not because it makes the code any better). If you prefer your way and the method in the example I provided, that's perfectly acceptable as well.

    Good luck!

  • Users who have thanked bacterozoid for this post:

    squidnunc (09-14-2009)

  • #7
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Function to convert MYSQL results to SEO-friendly URL's

    Using your latest suggestion, I've changed to the following code:

    PHP Code:
    <?php
    function cleanurl($string)
    {
    $cleanurl strtolower($string);
    $patterns = array('/[^a-z0-9\s]/''/\s\s+/''/\s/''/^-/''/-$/');
    $replacements = array(' '' ''-''''');
    $cleanurl preg_replace($patterns$replacements$cleanurl);
    return 
    $cleanurl;
    }
    ?>
    It still works and it makes for a smaller but slightly harder to read function. I like it, though, because I too like to have as few lines as possible which is why I asked this question to begin with. I believe this is now as concise as possible. Thanks again, bacterozoid!
    Last edited by squidnunc; 09-14-2009 at 09:13 PM.

  • #8
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I know this is an old and resolved thread, but I thought that anyone stumbling across this might like even more concise code
    PHP Code:
    $cleanurl strtolower($string);
    $cleanurl preg_replace('/[^a-z0-9]+/''-'$cleanurl);
    $cleanurl trim($cleanurl'-'); 
    The preg_replace replaces any sequence of one or more non-alphanumeric chars with a single hyphen, merging the first three steps of the algorithm. The trim removes any hyphens at the beginning or end of the string, replacing the last two steps. To put it all in a single line the function could look like this
    PHP Code:
    function cleanurl($string$join '-')
    {
      return 
    trim(preg_replace('/[^a-z0-9]+/'$joinstrtolower($string)),$join);



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •