大家好,我有 CSS 代碼,我正在嘗試找到一種方法來僅獲取 CSS 類的名稱,并清除 coma 和 open&close 標記和值,并將其放入 PHP 中的陣列中
例子:
.dungarees {
content: "\ef04";
}
.jacket {
content: "\ef05";
}
.jumpsuit {
content: "\ef06";
}
.shirt {
content: "\ef07";
}
我想用 PHP 做一個函式來把它轉換成這樣的陣列
$my_array('dungarees','jacket','jumpsuit','shirt');
php甚至jquery有什么功能可以處理這個問題嗎?謝謝
uj5u.com熱心網友回復:
您可以使用簡單的正則運算式創建這樣的陣列。
$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);
并且會導致
Array
(
[0] => dungarees
[1] => jacket
[2] => jumpsuit
[3] => shirt
)
uj5u.com熱心網友回復:
逐行掃描字串,期望它以開頭.和結尾{
<?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));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/497881.html
標籤:javascript php jQuery css
