Skip to content

Instantly share code, notes, and snippets.

@zeshanshani
Created March 17, 2020 23:24
Show Gist options
  • Save zeshanshani/2270718f6d96f45820af80385799626a to your computer and use it in GitHub Desktop.
Save zeshanshani/2270718f6d96f45820af80385799626a to your computer and use it in GitHub Desktop.
<?php
/**
* Convert String to ID
*
* @param string $string
* @return string
*/
function zawp_convert_to_id( $string ) {
// Lower case everything
$string = strtolower( $string );
// Make alphanumeric (removes all other characters)
$string = preg_replace( "/[^a-z0-9_\s-]/", "", $string );
// Clean up multiple dashes or whitespaces
$string = preg_replace( "/[\s-]+/", " ", $string );
// Convert whitespaces and underscore to dash
$string = preg_replace( "/[\s_]/", "-", $string );
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment