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

この正規表現の怪物には、正規表現の完全な説明がありますが、関数の説明はありません。失敗。

この関数は次のことを行います。

  1. $text 内のタグを検索します。script タグには、任意の数の属性を含めることができます。スクリプト タグの間にはコンテンツがなく、空白 (Fail) もありません。
  2. 重複する src 属性 $x を持つタグを削除します。他の属性は無視されます。

スタイルタグでは何もしません。thedailywtf.comに送信して使用する予定がない限り、使用することはお勧めしません.

于 2013-10-16T15:07:50.143 に答える