How I can make a clean url from text

Discussion in 'PHP' started by dan101, Oct 25, 2009.

  1. #1
    Hi

    This is how I need. Let's say that my title is

    $title="Some text here with many characters,.?/!@#$%^&*()_+| bla bla123";
    Code (markup):
    I want a function that will return only : some-text-here-with-many-characters-bla-bla123

    As you can see this will keep only the letters and numbers. I don't need the other characters.

    I tried this but is not good

    function cleanurl ( $str ) {
    $str = urlencode($str);
    return $str = str_replace( '+', '-', $str );
    }
    Code (markup):

    This replace the spaces with "-" and encode the other characters
     
    dan101, Oct 25, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $title="Some text here with many characters,.?/!@#$%^&*()_+| bla bla123";
    $result = strtolower(preg_replace('/[^a-z\d]+/i', '-', $title));
    PHP:
     
    JAY6390, Oct 25, 2009 IP
  3. dan101

    dan101 Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Great .. works perfect .. Just a little bug

    If I have the title $title="Some text here with many ” fdg" , How I can do widhout ” ?
     
    dan101, Oct 25, 2009 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
  5. dan101

    dan101 Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I tried this but with no results

     
    dan101, Oct 25, 2009 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    obviously you would need to run html_entity_decode before the replacement of characters

    $title="Some text here with many ©characters,.?/!@#$%^&*()_+| bla bla123";
    $result = strtolower(preg_replace('/[^a-z\d]+/i', '-', html_entity_decode($title)));
    echo $result;
    PHP:
     
    JAY6390, Oct 25, 2009 IP
  7. dan101

    dan101 Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This works great as before ..

    I take you to the limit .. If I have this
    .. The <b>,</b> should not appear .. I think can be done with strip_tags
     
    dan101, Oct 25, 2009 IP