Convert string to underscore_name


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



Copy this code and paste it in your HTML
  1. <?php
  2. /* Convert string to underscore_name
  3.  
  4. Converts "My House" to "my_house".
  5. Converts " Peter's nice car " to "peters_nice_car".
  6. Converts "_88" to "88" */
  7.  
  8. function string_to_underscore_name($string)
  9. {
  10. $string = preg_replace('/[\'"]/', '', $string);
  11. $string = preg_replace('/[^a-zA-Z0-9]+/', '_', $string);
  12. $string = trim($string, '_');
  13. $string = strtolower($string);
  14.  
  15. return $string;
  16. }
  17. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.