Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate a style tag based on an associative array of styles
<?php
$styles = [
'color' => '#f00',
'background' => '#fff',
];
cameronjonesweb_generate_style_attribute( $styles );
<?php
/**
* Generates a style tag based off an associative array of styles
*
* @param array $styles
*/
function cameronjonesweb_generate_style_attribute( $styles ) {
return ! empty( $styles ) ? sprintf( 'style="%1$s"', implode( array_map( function( $key, $value ) {
return $key . ':' . $value . ';';
}, array_keys( $styles ), $styles ) ) ) : '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.