hello guyes I have CSS code and I'm trying to find a way to get only the CSS Class's name Only and clear coma and open&close tag and value and put it into an array in PHP
Example:
.dungarees {
content: "\ef04";
}
.jacket {
content: "\ef05";
}
.jumpsuit {
content: "\ef06";
}
.shirt {
content: "\ef07";
}
and I want to do a a function with PHP to convert it into an array like this
$my_array('dungarees','jacket','jumpsuit','shirt');
is there any function with php or even jquery to deal with this? thanks
You can create such an array with a simple Regex.
$cssText = <<<'_CSS'
.dungarees {
content: "\ef04";
}
.jacket {
content: "\ef05";
}
.jumpsuit {
content: "\ef06";
}
.shirt {
content: "\ef07";
}
_CSS;
$matches = [];
preg_match_all('/\.([\w\-]+)/', $cssText, $matches);
$myArray = $matches[1];
print_r($myArray);
And will result in
Array
(
[0] => dungarees
[1] => jacket
[2] => jumpsuit
[3] => shirt
)
Scan the string line by line, expecting it to begin with . and end with {
<?php
$result = [];
$content_of_css = '
.dungarees {
content: "\ef04";
}
.jacket {
content: "\ef05";
}
.jumpsuit {
content: "\ef06";
}
.shirt {
content: "\ef07";
}
';
// or $content_of_css = file_get_contents("path_to_css");
$arr = explode("\n", $content_of_css);
foreach ($arr as $line) {
$line = trim($line);
if (strrpos($line, ".") === 0) {
$result[] = trim(substr($line, 1, strlen($line) - 2));
}
}