Posted By

BFTrick on 12/23/09


Tagged

url clean


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

latavish
rene-design
BrianCoyDesign


clean Url


 / Published in: PHP
 

Use this function to transform a title into a url friendly title

  1. function cleanUrl( $url ){
  2.  
  3. $new_url = str_replace( " ", "-", $url );
  4. $new_url = str_replace( "/", "-", $new_url );
  5. $new_url = str_replace( "--", "-", $new_url );
  6. $new_url = str_split( $new_url );
  7. $new_url = preg_grep( "<[A-Za-z0-9-_]+>", $new_url );
  8. $new_url = trim( implode( $new_url ) );
  9. $new_url = strtolower($new_url);
  10.  
  11. // Was still getting cases of double hyphens
  12. $new_url = str_replace( "--", "-", $new_url );
  13.  
  14. return $new_url;
  15.  
  16. }// end function - cleanUrl

Report this snippet  

You need to login to post a comment.