저는 이것이 아주 단순 할 것이라고 생각했지만, 지금은 잠시 동안 고민하고 있습니다. 거기에 CSS 파서 클래스가 있다는 것을 알고 있습니다. 원하는 기능을 수행 할 수는 있지만 95 %의 기능을 필요로하지 않으므로 실제로 실행 가능하지 않고 너무 무거울 수 있습니다.소스 코드에서 PHP 클래스와 ID를 추출하십시오.
정규식을 통해 CSS 파일에 사용 된 모든 클래스 및/또는 ID 이름을 제거 할 수 있어야합니다. 다음은 정규식입니다. 은 작동하지만 아직 사용하지 않았습니다. 내 샘플에 대해 실행
[^a-z0-9][\w]*(?=\s)
:
.stuffclass {
color:#fff;
background:url('blah.jpg');
}
.newclass{
color:#fff;
background:url('blah.jpg');
}
.oldclass {
color:#fff;
background:url('blah.jpg');
}
#blah.newclass {
color:#fff;
background:url('blah.jpg');
}
.oldclass#blah{
color:#fff;
background:url('blah.jpg');
}
.oldclass #blah {
color:#fff;
background:url('blah.jpg');
}
.oldclass .newclass {
text-shadow:1px 1px 0 #fff;
color:#fff;
background:url('blah.jpg');
}
.oldclass:hover{
color:#fff;
background:url('blah.jpg');
}
.newclass:active {
text-shadow:1px 1px 0 #000;
}
이 일치 대부분 내가 원하는 걸의, 그러나 또한 중괄호를 포함한 것와 ID의 일치하지 않는 않습니다. 결합시 ID와 클래스를 따로 일치시켜야합니다. 따라서 기본적으로 #blah.newclass
은 #blah
및 .newclass
의 2 개의 개별적인 일치가됩니다.
아이디어가 있으십니까?
===================
내가 처음 {
및 }
사이에 모든 것을 제거하기 위해 2 정규식을 사용하여 상처
최종 솔루션, 다음 나머지 입력을 기반으로 셀렉터를 간단히 일치시킵니다.
여기에 전체 작업 예제 :
//Grab contents of css file
$file = file_get_contents('css/style.css');
//Strip out everything between { and }
$pattern_one = '/(?<=\{)(.*?)(?=\})/s';
//Match any and all selectors (and pseudos)
$pattern_two = '/[\.|#][\w]([:\w]+?)+/';
//Run the first regex pattern on the input
$stripped = preg_replace($pattern_one, '', $file);
//Variable to hold results
$selectors = array();
//Run the second regex pattern on $stripped input
$matches = preg_match_all($pattern_two, $stripped, $selectors);
//Show the results
print_r(array_unique($selectors[0]));
A [완전한 CSS 파서]을 사용하지 왜 (http://stackoverflow.com/questions/4886067/css-parser-using-php) 선택기를 추출하려면? – mario
CSS 파서의 문제점은 무엇입니까? 벤치 마크를 실행 했습니까? "너무 무겁다"고 생각하기 때문에 배제하지 마십시오. – Pete
lol doh! 나는 내 이름을 잘못 썼다. 위에서 언급했듯이, 내가하고 싶은 일을하기에는 너무 무겁고 부풀어 오르기 때문에 나는 완벽한 CSS 파서를 사용하지 않는다 ... 나는 결코 사용하지 않을 톤의 기능을 포함하고있다. 간단한 한 줄 정규식을 사용하면 효과적 일 수 있습니다. –