Skip to content

Instantly share code, notes, and snippets.

@cameronjonesweb
Created January 29, 2019 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cameronjonesweb/952d595b50a3db0b3a9cef8d37fd658a to your computer and use it in GitHub Desktop.
Save cameronjonesweb/952d595b50a3db0b3a9cef8d37fd658a to your computer and use it in GitHub Desktop.
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