Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (?<selector>[A-Za-z]+[s]*)[s]*{[s]*((?<properties>[A-Za-z0-9-_]+)[s]*:[s]*(?<values>[A-Za-z0-9#, ]+);[s]*)*[s]*}
- body { background: #f00; font: 12px Arial; }
- Array(
- [0] => Array(
- [0] => body { background: #f00; font: 12px Arial; }
- [selector] => Array(
- [0] => body
- )
- [1] => Array(
- [0] => body
- )
- [2] => font: 12px Arial;
- [properties] => Array(
- [0] => font
- )
- [3] => Array(
- [0] => font
- )
- [values] => Array(
- [0] => 12px Arial
- [1] => background: #f00
- )
- [4] => Array(
- [0] => 12px Arial
- [1] => background: #f00
- )
- )
- )
- Array(
- [0] => Array
- (
- [0] => body { background: #f00; font: 12px Arial; }
- [selector] => body
- [1] => body
- [2] => font: 12px Arial;
- [properties] => font
- [3] => font
- [values] => 12px Arial
- [4] => 12px Arial
- )
- )
- /([^{])s*{s*([^}])s*}/
- $cssp = new cssparser;
- $cssp -> ParseStr("body { background: #f00;font: 12px Arial; }");
- print_r($cssp->css);
- Array
- (
- [body] => Array
- (
- [background] => #f00
- [font] => 12px arial
- )
- )
- $css_array = array(); // master array to hold all values
- $element = explode('}', $css);
- foreach ($element as $element) {
- // get the name of the CSS element
- $a_name = explode('{', $element);
- $name = $a_name[0];
- // get all the key:value pair styles
- $a_styles = explode(';', $element);
- // remove element name from first property element
- $a_styles[0] = str_replace($name . '{', '', $a_styles[0]);
- // loop through each style and split apart the key from the value
- $count = count($a_styles);
- for ($a=0;$a<$count;$a++) {
- if ($a_styles[$a] != '') {
- $a_key_value = explode(':', $a_styles[$a]);
- // build the master css array
- $css_array[$name][$a_key_value[0]] = $a_key_value[1];
- }
- }
- }
- Array
- (
- [body] => Array
- (
- [background] => #f00
- [font] => 12px arial
- )
- )
- function trimStringArray($stringArray){
- $result = array();
- for($i=0; $i < count($stringArray); $i++){
- $trimmed = trim($stringArray[$i]);
- if($trimmed != '') $result[] = $trimmed;
- }
- return $result;
- }
- $regExp = '/{|}/';
- $rawCssData = preg_split($regExp, $style);
- $cssArray = array();
- for($i=0; $i < count($rawCssData); $i++){
- if($i % 2 == 0){
- $cssStyle['selectors'] = array();
- $selectors = split(',', $rawCssData[$i]);
- $cssStyle['selectors'] = trimStringArray($selectors);
- }
- if($i % 2 == 1){
- $attributes = split(';', $rawCssData[$i]);
- $cssStyle['attributes'] = trimStringArray($attributes);
- $cssArray[] = $cssStyle;
- }
- }
- //return false;
- echo '<pre>'."n";
- print_r($cssArray);
- echo '</pre>'."n";
- (?<selector>(?:(?:[^,{]+),?)*?){(?:(?<name>[^}:]+):?(?<value>[^};]+);?)*?}
Add Comment
Please, Sign In to add comment