I need to get css class name, attributes and values in order to apply inline css style to a div. Currently, I can retrive css class name information from a string like this :
$class = ".custom_class {
margin-top: 0px !important;
border-top-width: 0px !important;
border-right-width: 0px !important;
border-left-width: 0px !important;
padding-top: 250px !important;
padding-bottom: 250px !important;
background-image: url(http://localhost/wordpress/wp-content/uploads/2015/01/old-car.jpg?id=3663) !important;
border-left-color: #dd3333 !important;
border-right-color: #dd3333 !important;
border-top-color: #dd3333 !important;
}";
How can I get or regex all css attributes and values in order to include them in a div like this:
<div class="custom_class" style="margin-top: 0px !important;border-top-width: 0px !important;border-right-width: 0px !important;border-left-width: 0px !important;padding-top: 250px !important;padding-bottom: 250px !important;background-image: url(http://localhost/wordpress/wp-content/uploads/2015/01/old-car.jpg?id=3663) !important;border-left-color: #dd3333 !important;border-right-color: #dd3333 !important;border-top-color: #dd3333 !important;"></div>
I tried with this: preg_match("/{(.*?)}/", $class, $match);
But it outputs with brackets. And for the class name I don't know how to do that.
ou're near with your regex preg_match("/{(.*?)}/", $class, $match);
, just escape the curly brackets and retrive the data in $match[1]
:
preg_match("/^([\w.]+)\s*\{(.*?)\}/s", $class, $match);
$classname = $match[1];
$style = $match[2];