pip.sanitize_title()
Heads up!
This article contains Javascript code and is intended for developers. We offer this code as a courtesy, but don’t provide support for code customizations or 3rd party development.
Javascript equivalent of PHP’s sanitize_title()
/**
* Sanitize value like WP function "sanitize_title"
*
* @param $val
*
* @returns {string}
*/
pip.sanitize_title = function ( $val ) {
return $val
.toLowerCase()
.replace( /\s+/g, '-' ) // Replace spaces with -
.replace( /\-\-+/g, '-' ) // Replace multiple - with single -
.replace( /\_\_+/g, '_' ) // Replace multiple _ with single _
.replace( /^-+/, '' ) // Trim - from start of text
.normalize( 'NFD' ) // Change accent to unicode value
.replace( /[\u0300-\u036f]/g, '' ) // From unicode value to letter
.replace( /[^a-zA-Z0-9_\-\s]+/g, '' ); // Remove all non-word chars
};
// To use it
let example_string = pip.sanitize_title( 'Example string' );
// example_string = "examplestring"
Our home port
15 Rue Cavenne, 69007 Lyon
And from everywhere in the world thanks to Hangout and Skype !
Useful links