Skip to content

Instantly share code, notes, and snippets.

@essamamdani
Last active February 24, 2019 13:33
Show Gist options
  • Select an option

  • Save essamamdani/299a31c01b899c3ca42a66c5266e8df5 to your computer and use it in GitHub Desktop.

Select an option

Save essamamdani/299a31c01b899c3ca42a66c5266e8df5 to your computer and use it in GitHub Desktop.
PHP code of simple little tool is intended to help translate plain CSS into the React in-line style specific JSON representation. Making it easy to copy and paste into an inline React component.
function inlineCSStoReact($normalcss){
$json = preg_replace_callback(
"|(\w+)\-?(\w+)?\s*:\s*([^;]+)\s*;?|",
function($matches){
return '"'.$matches[1].ucfirst($matches[2]).'":"'.$matches[3].'",';
},
$normalcss);
return "{".rtrim($json,",")."}";
}
$inlineCSS = "background-image: url(https://source.unsplash.com/random);font-family:arial;text-align:center;display:block;";
echo inlineCSStoReact($text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment