Posted By

alyssonweb on 07/07/11


Tagged

url String uri friendly Friend


Versions (?)

To Friendly String


 / Published in: PHP
 

Convert a string to friendly string.

  1. if ( ! function_exists( 'to_friendly_string' ) ) :
  2.  
  3. function to_friendly_string( $phrase = '', $maxLength = false ) {
  4. if ( empty( $phrase ) )
  5. return '';
  6.  
  7. $result = strtolower( $phrase );
  8.  
  9. $result = trim( preg_replace( "/&{1}(amp;)?/", ' and ', $result ) );
  10. $result = trim( preg_replace( "/[^a-z0-9\s-]/", '', $result ) );
  11. $result = trim( preg_replace( "/[\s-]+/", ' ', $result ) );
  12.  
  13. if ( $maxLength )
  14. $result = trim( substr( $result, 0, $maxLength ) );
  15.  
  16. $result = preg_replace( "/\s/", "-", $result );
  17.  
  18. return $result;
  19. }
  20.  
  21. endif;

Report this snippet  

You need to login to post a comment.