Skip to content

Instantly share code, notes, and snippets.

@ixkaito
Created October 21, 2023 02:37
Show Gist options
  • Save ixkaito/9fd861a517e7ddad8e311b0cd282c69a to your computer and use it in GitHub Desktop.
Save ixkaito/9fd861a517e7ddad8e311b0cd282c69a to your computer and use it in GitHub Desktop.
PHP clsx inspired by https://github.com/lukeed/clsx.
<?php
/**
* Takes any number of class names or arrays of class names and returns a string of unique class names.
*
* @param mixed ...$classnames Any number of class names or arrays of class names.
*
* @return string A string of unique class names.
*/
function clsx( ...$classnames ) {
$classnames = array_filter(
$classnames,
function ( $classname ) {
return ! empty( $classname ) && is_string( $classname );
}
);
$classnames = array_map(
function ( $classname ) {
return preg_replace( '/\s+/S', ' ', trim( $classname ) );
},
$classnames
);
return implode( ' ', $classnames );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment