PHP SEF Clean url function

You may need this php function when you want to clean your url especially when extracting data from mysql will bring special characters like !, ?, @ … The url must not contain characters like those and you must replace them like in the example. SEF stands for Search Engine Friendly, so this php function will create a SEF url by cleaning it up of those unwanted characters.

Example
without cleaning your url will look like: /Mail-@-you-stopped!.php (this is not right)
with PHP SEF clean url will be like: /mail-you-stopped.php (the correct way)


PHP clean url code

  1. <?php
  2. function clean_url($text)
  3. {
  4. $text=strtolower($text);  // returns string converted to lowercase.
  5. $code_entities_match = array( '"' ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&amp;' ,'*' ,'(' ,')' ,'+' ,'{' ,'}' ,'|' ,':' ,'"' ,'&lt;' ,'&gt;' ,'?' ,'[' ,']' ,'' ,';' ,"'" ,',' ,'.' ,'_' ,'/' ,'*' ,'+' ,'~' ,'`' ,'=' ,' ' ,'—' ,'–','–');
  6. $code_entities_replace = array('' ,'-' ,'-' ,'' ,'' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'-' ,'-' ,'-','-');
  7. $text = str_replace($code_entities_match, $code_entities_replace, $text);  // for replacing
  8. return $text;
  9. }
  10. ?>

Related php tutorials:

  1. PHP sef url function

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark
tabs-top

Leave a Reply