Php 正则表达式替换脚本标记外的文本
我有这个HTML: "This is simple html text <script language="javascript">simple simple text text</script> text" “这是简单的html文本” 我只需要匹配脚本标记之外的单词。我的意思是,如果我想匹配“simple”和“text”,我应该只从“This is simple html text”和最后一部分“text”得到结果-结果将是“simple”1匹配,“text”2匹配。有人能帮我吗?我正在使用PHP 我在标记外的匹配文本中找到了类似的答案:Php 正则表达式替换脚本标记外的文本,php,html,regex,preg-replace,Php,Html,Regex,Preg Replace,我有这个HTML: "This is simple html text <script language="javascript">simple simple text text</script> text" “这是简单的html文本” 我只需要匹配脚本标记之外的单词。我的意思是,如果我想匹配“simple”和“text”,我应该只从“This is simple html text”和最后一部分“text”得到结果-结果将是“simple”1匹配,“text”2匹配。有
(text|simple)(?![^<]*>|[^<>]*</)
(text | simple)(?![^ |[^]*如果确定脚本将出现,则只需与
(.*?)<script.*</script>(.*)
(.*)这里是另一个解决方案
([\w\s]*)(?:<script.*?\/script>)(.*)$
([\w\s]*)(?:)(.*)$
这是一个演示,仅供参考,就标签而言,不可能忽略一个标签
不分析所有标记。
您可以跳过html标记和不可见内容。
这会找到你要找的词
目前,在该地区的代码>“<代码>””“““[S\S\S\S\S\S\S\S\S\S”之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以,,,,,,,,,,,,,,,,“[目前目前(州州州州州:)除除除除除除除除除除上述上述上述上述上述上述几几几几几几几几几几几几名))除除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了其他其他其他其他其他其他其他几几几几几几几几几几几几几几几几几几名,”,”外,,,,,,,,,,,,,,,,,,,,,[CDATA\[\S\S]*?\]\])(?:--[\S\S]*?-)(?:ATTLIST[\S\S]*?)(?:实体[\S\S]*?)(?:元素[\S\S]*)>(*跳过)(?!)(?:文本简单)~'
形成
<
(?:
(?:
(?:
# Invisible content; end tag req'd
( # (1 start)
script
| style
| object
| embed
| applet
| noframes
| noscript
| noembed
) # (1 end)
(?:
\s+
(?>
" [\S\s]*? "
| ' [\S\s]*? '
| (?:
(?! /> )
[^>]
)?
)+
)?
\s* >
)
[\S\s]*? </ \1 \s*
(?= > )
)
| (?: /? [\w:]+ \s* /? )
| (?:
[\w:]+
\s+
(?:
" [\S\s]*? "
| ' [\S\s]*? '
| [^>]?
)+
\s* /?
)
| \? [\S\s]*? \?
| (?:
!
(?:
(?: DOCTYPE [\S\s]*? )
| (?: \[CDATA\[ [\S\s]*? \]\] )
| (?: -- [\S\s]*? -- )
| (?: ATTLIST [\S\s]*? )
| (?: ENTITY [\S\s]*? )
| (?: ELEMENT [\S\s]*? )
)
)
)
>
(*SKIP)
(?!)
|
(?: text | simple )
<
(?:
(?:
(?:
#不可见内容;需要结束标记
(#(1开始)
剧本
|风格
|反对
|嵌入
|小程序
|无框
|noscript
|诺姆贝德
)#(一完)
(?:
\s+
(?>
“[\S\S]*?”
|“[\S\S]*?”
| (?:
(?! /> )
[^>]
)?
)+
)?
\s*>
)
[\S\S]*?)
)
|(?:/?[\w::+\s*/?)
| (?:
[\w:]
\s+
(?:
“[\S\S]*?”
|“[\S\S]*?”
| [^>]?
)+
\s*/?
)
|\?[\S\S]*?\?
| (?:
!
(?:
(?:DOCTYPE[\S\S]*?)
|(?:\[CDATA\[\S\S]*?\]\]
|(?:--[\S\S]*?--)
|(?:ATTLIST[\S\S]*?)
|(?:实体[\S\S]*?)
|(?:元素[\S\S]*?)
)
)
)
>
(*跳过)
(?!)
|
(?:文本|简单)
或者,一种更快的方法是同时匹配标记和文本
寻找
匹配标记会经过它们
如果要进行替换,请使用回调来确定要替换的内容。
组1是标记或不可见的内容运行。
第三组是你要替换的词
因此,在回调中,如果组1匹配,只返回组1。
如果第3组匹配,则替换为您想要替换的组
正则表达式
在本次代码发布过程中,除了除了上述两个国家的代码外,其他一些国家的代码还将在以下两个州的““,,,,,,,,,,,,,,,,,,,除除除除上述两个国家的\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S)除上述上述上述两个国家的,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\[CDATA\[\S\S]*?\]\]。(?:--[\S\S]*?-)。(?:ATTLIST[\S\S]*?)。(?:实体[\S\S]*?)。(?:元素[\S\S]*?)>)。(文本简单)~'
此正则表达式与SAX和DOM解析器解析标记的方式相当。
我已经在网上贴了几百次了
以下是如何删除所有html标记的示例:
我的模式将使用(*跳过)(*失败)
取消匹配脚本标记及其内容的资格
text
和simple
将在每次符合条件的事件中匹配
正则表达式模式:~(*跳过)(*失败)|文本|简单~
代码:()
$strings=['这没有替代品',
'此简单文本没有脚本标记',
'此简单文本以脚本标记simple text'结尾,
'这是简单html文本被脚本标记简单文本拆分',
'简单文本此文本以脚本标记开头'
];
$strings=preg_replace(“~(*SKIP)(*FAIL)| text | simple~”、“***已替换***”、$strings);
var_导出(字符串);
输出:
array (
0 => 'This has no replacements',
1 => 'This ***replaced*** ***replaced*** has no script tag',
2 => 'This ***replaced*** ***replaced*** ends with a script tag <script language="javascript">simple simple text text</script>',
3 => 'This is ***replaced*** html ***replaced*** is split by a script tag <script language="javascript">simple simple text text</script> ***replaced***',
4 => '<script language="javascript">simple simple text text</script> this ***replaced*** starts with a script tag',
)
数组(
0=>“这没有替换项”,
1=>“此***已替换******已替换***没有脚本标记”,
2=>“此***替换******替换***以脚本标记simple text结尾”,
3=>'这是***替换***html***替换***被脚本标记简单文本文本***替换***'拆分,
4=>“此***替换的简单文本***以脚本标记开头”,
)
您是否绝对需要匹配,或者捕获组就可以了?当您想要自信地解析html时,请使用html解析器而不是regex。因此,这一点一再重复。IIRC甚至会弹出一条注释,说明SO软件“不要使用regex解析html”@mickmackusa,但当你使用解析器时,他们会停止解析格式错误的html。我认为这个问题不是重复的。因为我没有试图剥离标签,我只是试图替换标签“脚本”之外的内容.Retracted dupe link,它只是相关的。我正在尝试替换标记外的字符串。是的,这是在组1中捕获的,因为regex101突出显示这是简单的html文本,匹配2位于标记内,并且没有选择最后一个单词“text”。最后,这是尝试忽略所有标记,而不是指定的标记“script”.哈..我明白了问题所在…我错过了第二段文字。我更新了答案和r
$strings=['This has no replacements',
'This simple text has no script tag',
'This simple text ends with a script tag <script language="javascript">simple simple text text</script>',
'This is simple html text is split by a script tag <script language="javascript">simple simple text text</script> text',
'<script language="javascript">simple simple text text</script> this text starts with a script tag'
];
$strings=preg_replace('~<script.*?/script>(*SKIP)(*FAIL)|text|simple~','***replaced***',$strings);
var_export($strings);
array (
0 => 'This has no replacements',
1 => 'This ***replaced*** ***replaced*** has no script tag',
2 => 'This ***replaced*** ***replaced*** ends with a script tag <script language="javascript">simple simple text text</script>',
3 => 'This is ***replaced*** html ***replaced*** is split by a script tag <script language="javascript">simple simple text text</script> ***replaced***',
4 => '<script language="javascript">simple simple text text</script> this ***replaced*** starts with a script tag',
)