Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用Drupal7的正则表达式_Regex_Drupal - Fatal编程技术网

Regex 使用Drupal7的正则表达式

Regex 使用Drupal7的正则表达式,regex,drupal,Regex,Drupal,我发现了这段代码,它替换了dom中的多个重复脚本和样式标记, 但我不知道如何使用它 我正在使用Drupal7构建我的站点 如果可以的话,请帮忙 非常感谢你 php代码: function stripDuplicateScripts($text) { $re = '% # Match duplicate SCRIPT element having same SRC attribute URL. ( # $1: Everything up to d

我发现了这段代码,它替换了dom中的多个重复脚本和样式标记, 但我不知道如何使用它

我正在使用Drupal7构建我的站点

如果可以的话,请帮忙

非常感谢你

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;
函数stripDuplicateScripts($text){
$re='%
#匹配具有相同SRC属性URL的重复脚本元素。
(#$1:duplicate SCRIPT元素之前的所有内容。

这个怪物对正则表达式有一个完整的解释,但没有对函数的解释。失败

该函数执行以下操作:

  • 它在$text中搜索标记。脚本标记可以有任意数量的属性。脚本标记之间不能有任何内容,甚至不能有空格(Fail)
  • 它删除任何具有重复src属性$x的标记。它忽略其他属性

  • 它与样式标记无关。我不建议使用它,除非您打算通过将其发送到dailywtf.com来使用它。

    我曾尝试将此代码转换为html.tpl.php,但没有转换为worksWow,这看起来很不神圣。查看。阅读以了解为什么regex不是解析html的好选择