1
dom에 여러 개의 중복 스크립트 및 스타일 태그를 대체하는이 코드를 발견했지만 사용 방법은 모르겠다.regex drupal을 사용하여 7
drupal 7을 사용하여 사이트를 구축하고 있습니다.
가능한 경우 도움을 받으십시오.
당신에게 많이 감사합니다 ..PHP 코드 : 함수의
function stripDuplicateScripts($text) {
$re = '%
# Match duplicate SCRIPT element having same SRC attribute URL.
( # $1: Everything up to duplicate SCRIPT element.
<script # literal start of script open tag
(?: # Zero or more attributes before SRC.
\s+ # Whitespace required before attribute.
(?!src\b) # Assert this attribute is not "SRC".
[\w\-.:]+ # Non-SRC attribute name.
(?: # Attribute value is optional.
\s*=\s* # Value separated by =, optional ws.
(?: # Group attribute value alternatives.
"[^"]*" # Either a double quoted value,
| \'[^\']*\' # or a single quoted value,
| [\w\-.:]+ # or an unquoted value.
) # End group of value alternatives.
)? # Attribute value is optional.
)* # Zero or more attributes before SRC.
\s+ # Whitespace required before SRC attrib.
src # Required SRC attribute name.
\s*=\s* # Value separated by =, optional ws.
([\'"]) # $2: Attrib value opening quote.
((?:(?!\2).)+) # $3: SRC attribute value (a URL).
\2 # Attrib value closing quote.
(?: # Zero or more attributes after SRC.
\s+ # Whitespace required before attribute.
[\w\-.:]+ # Attribute name.
(?: # Attribute value is optional.
\s*=\s* # Value separated by =, optional ws.
(?: # Group attribute value alternatives.
"[^"]*" # Either a double quoted value,
| \'[^\']*\' # or a single quoted value,
| [\w\-.:]+ # or an unquoted value.
) # End group of value alternatives.
)? # Attribute value is optional.
)* # Zero or more attributes after SRC.
\s* # Optional whitespace before tag close.
> # End of SCRIPT open tag.
</script\s*> # SCRIPT close tag.
.*? # Stuff up to duplicate script element.
) # End $1: Everything up to duplicate SCRIPT.
<script # literal start of script open tag
(?: # Zero or more attributes before SRC.
\s+ # Whitespace required before attribute.
(?!src\b) # Assert this attribute is not "SRC".
[\w\-.:]+ # Non-SRC attribute name.
(?: # Attribute value is optional.
\s*=\s* # Value separated by =, optional ws.
(?: # Group attribute value alternatives.
"[^"]*" # Either a double quoted value,
| \'[^\']*\' # or a single quoted value,
| [\w\-.:]+ # or an unquoted value.
) # End group of value alternatives.
)? # Attribute value is optional.
)* # Zero or more attributes before SRC.
\s+ # Whitespace required before SRC attrib.
src # Required SRC attribute name.
\s*=\s* # Value separated by =, optional ws.
([\'"]) # $4: Attrib value opening quote.
\3 # This script must have duplicate SRC URL.
\4 # Attrib value closing quote.
(?: # Zero or more attributes after SRC.
\s+ # Whitespace required before attribute.
[\w\-.:]+ # Attribute name.
(?: # Attribute value is optional.
\s*=\s* # Value separated by =, optional ws.
(?: # Group attribute value alternatives.
"[^"]*" # Either a double quoted value,
| \'[^\']*\' # or a single quoted value,
| [\w\-.:]+ # or an unquoted value.
) # End group of value alternatives.
)? # Attribute value is optional.
)* # Zero or more attributes after SRC.
\s* # Optional whitespace before tag close.
> # End of SCRIPT open tag.
</script\s*> # SCRIPT close tag.
\s* # Strip whitespace following duplicate.
%six';
while (preg_match($re, $text)) {
$text = preg_replace($re, '$1', $text);
}
return $text;
}
이 코드를 html.tpl.php에 넣으려고했으나 제대로 작동하지 않았습니다. –
와우, 그럴 수 없습니다. [DomDocument] (http://php.net/manual/en/class.domdocument.php)를 살펴보십시오. regex가 html을 구문 분석하기위한 좋은 선택이 아닌 이유를 이해하려면 [this] (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)를 읽으십시오. – gwillie