2013-10-16 2 views
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; 

}

+0

이 코드를 html.tpl.php에 넣으려고했으나 제대로 작동하지 않았습니다. –

+0

와우, 그럴 수 없습니다. [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

답변

0

정규식의이 괴물은 정규 표현식에 대한 자세한 설명이 있지만, 아무런 설명. 실패.

기능은 다음 작업을 수행합니다

그것은 $ 텍스트에 태그를 검색
  1. . 스크립트 태그는 모든 양의 속성을 가질 수 있습니다. 스크립트 태그 사이에는 공백 (실패)이 아닌 내용이 없어야합니다.
  2. 중복 된 src 속성 $ x가있는 태그를 제거합니다. 다른 속성을 무시합니다.

스타일 태그에는 아무런 영향을주지 않습니다. thedailywtf.com에 보내서 사용하지 않으려는 경우 사용하지 않는 것이 좋습니다.

관련 문제