我找到了这段代码,它替换了 dom 中的多个重复的脚本和样式标签,但我不知道如何使用它。
我正在使用 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;
}