Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Method for taking a string formatted as a css selector and breaking it down into id/classes/attributes/in-line styles
- * to use in the creatin of an element. I.E. "#id.class.class2[attribute=value]{ border: 1px solid blue; }"
- *
- * @ignore this is the original regex i wrote, which was awesome, but broke on some edge cases ...
- * "!(\#(.+?)(\.|\[|\{)){1,}!" => ' id="$2" $3', //ID
- * "!(\.(.*?)(\[|\{)){1,}!" => ' class="$2" $3', //CLASS
- * "!\[(.*?)=([^\[]*)\]!" => ' $1="$2" ', //ATTRS
- * "!\{(.*)\}!" => ' style="$1" ', //INLINE STYLE
- * "!\.([a-zA-Z_]+[\w\-]*)!" => ' $1', //SPECIFIC CLASSES
- *
- */
- protected static function selectors($str){
- $selectors = $attrs = $styles = "";
- //grab everything before the first attr "[" or the first style "{" as square brackets are not used in ids/classes
- $selectors = substr($str, 0, strcspn($str, "[{"));
- if(!empty($selectors)){
- //if an id is declared via "#" grab it
- if(strpos($selectors, "#") !== false){
- // $attrs["id"] = sprintf('id="%s"', trim(substr($selectors, 0, strcspn($selectors, ".")), "#"));
- $attrs["id"] = trim(substr($selectors, 0, strcspn($selectors, ".")), "#");
- }
- //if there are classes declared via "."
- if(strpos($selectors, ".") !== false){
- // $classes = sprintf('class="%s"', substr($selectors, strcspn($selectors, ".")));
- $classes = trim(strtr(substr($selectors, strcspn($selectors, ".")), '.', ' '));
- // $attrs["class"] = sprintf('class="%s"', $classes);
- $attrs["class"] = $classes;
- }
- }
- //break up the sumbitted string by the first "[" and the last "]" as square brackets are not used in in-line styles or ids/classes
- $attributes_string = substr($str, strpos($str, "["), (strrpos($str, "]") - strpos($str, "[") + 1));
- if(!empty($attributes_string)){
- //break the isolated attrs string by the "keys" i.e. "[key=" because in JSON text must be quoted and html arrays shouln't use "="'s in key names
- $attributes_array = preg_split("!\[([\w]+?)=!", $attributes_string, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
- if(!empty($attributes_array) && is_array($attributes_array)){
- //loop through our string pieces using the value of one as the key and the value of the next as the value
- while( ($key = current($attributes_array)) && ($value = next($attributes_array)) ){
- //our values will have a trailing "]" and we can't use trim() because our value might actually end in its own "]"
- // $attrs[$key] = sprintf('%s="%s"', $key, str_replace('"', '\"', substr($value, 0, (strlen($value) - 1))));
- $attrs[$key] = substr($value, 0, (strlen($value) - 1));
- //advance the array so we're not using previous values as current keys
- next($attributes_array);
- }
- }
- }
- //styles are easy as we simply breaking the main string at the LAST "{"
- // $attrs["style"] = sprintf('style="%s"', trim(strrchr($str, "{"), " {}"));
- $attrs["style"] = trim(strrchr($str, "{"), " {}");
- return $attrs;
- // return implode(" ", $attrs);
- }
Add Comment
Please, Sign In to add comment