To Friendly String


/ Published in: PHP
Save to your folder(s)

Convert a string to friendly string.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.