1

我找到了这段代码,它替换了 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;

}

4

1 回答 1

0

This monster of a regex has a full explanation of the regex, but no explanation of the function. Fail.

The function does the following:

  1. It searches for tags in $text. The script tag can have any amount of attributes. There can be no content between the script tags, not even whitespace (Fail).
  2. It removes any tag that has a duplicate src attribute $x. It ignores the other attributes.

It does nothing with style tags. I would not recommend using it, unless you intend to use it by sending it to thedailywtf.com.

于 2013-10-16T15:07:50.143 回答