Created
October 21, 2023 02:37
-
-
Save ixkaito/9fd861a517e7ddad8e311b0cd282c69a to your computer and use it in GitHub Desktop.
PHP clsx inspired by https://github.com/lukeed/clsx.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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