Skip to content

Instantly share code, notes, and snippets.

@pyronaur
Last active October 2, 2019 11:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyronaur/5387539 to your computer and use it in GitHub Desktop.
Save pyronaur/5387539 to your computer and use it in GitHub Desktop.
sanitize_html_class works just fine for a single class But sometimes le wild `<span class="blue hedgehog">` appears, which is when you need this function, to validate both blue and hedgehog, Because sanitize_html_class doesn't allow spaces (because it's meant for a single class)
<?php
if ( ! function_exists( "sanitize_html_classes" ) && function_exists( "sanitize_html_class" ) ) {
/**
* sanitize_html_class works just fine for a single class
* Some times le wild <span class="blue hedgehog"> appears, which is when you need this function,
* to validate both blue and hedgehog,
* Because sanitize_html_class doesn't allow spaces.
*
* @uses sanitize_html_class
* @param (mixed: string/array) $class "blue hedgehog goes shopping" or array("blue", "hedgehog", "goes", "shopping")
* @param (mixed) $fallback Anything you want returned in case of a failure
* @return (mixed: string / $fallback )
*/
function sanitize_html_classes( $class, $fallback = null ) {
// Explode it, if it's a string
if ( is_string( $class ) ) {
$class = explode(" ", $class);
}
if ( is_array( $class ) && count( $class ) > 0 ) {
$class = array_map("sanitize_html_class", $class);
return implode(" ", $class);
}
else {
return sanitize_html_class( $class, $fallback );
}
}
}
@MikeiLL
Copy link

MikeiLL commented Jul 21, 2015

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment