删除 xml 文件中的换行符,在标签和之间,保持结构
Remove line breaks in a xml file, in tags and between, keeping the structure
长标题:)
无论如何,我有很多 XML 文件想要即时清理,使用 PHP preg_replace RegEx 输出进行简单即时转换。
现在我无法使更改永久生效,所以我编写了一个 php 函数来遍历文件。
我无法修复的是 RegEx 模式。
https://regex101.com/r/bN5eF4/7
我要匹配:
<all-tags with-their="attribute"
even-if-there="are-more">
and all the content between the start and end tag
even if there
are line breaks
in between them
</all-tags>
我敢打赌这很简单,但我从来没有很好地处理 RegEx...遗憾的是。
已编辑
似乎人们希望我构建一个 SimpleXML 的解析器函数,它遍历 xml 文件并删除换行符?
在同一过程中,我想删除一些元素及其内容,具体取决于它们在属性中的内容。 分析可以这么说。
我认为在使用 Xsltprocessor 处理 xml 文件之前进行换行和分析会是更快的选择?
尝试以下正则表达式:
/(?<=\>)(\r?\n)|(\r?\n)(?=\<\/)/
在这里您要搜索 >
末尾或 </
开头的换行符,并将其替换为空字符串。
在 Regex101
查看演示
根据您的示例输入文本,它将删除所有换行符并发出如下内容:
<all-tags with-their="attribute" even-if-there="are-more">and all the content between the start and end tag</all-tags>
我设法用 2 个正则表达式模式做到了。
输入:
<all-tags
with-their="attribute"
even-if-there="are-more"
aa="1">
and all the content between
the start and end tag
</all-tags>
<meta-tag />
1。在打开标签之前和结束标签之后删除换行符 https://regex101.com/r/PPzkWv/2/
/(?<=\>)(\n+)|(\n+)(?=\<)/
输出:
<all-tags
with-their="attribute"
even-if-there="are-more"
aa="1">and all the content between
the start and end tag</all-tags><meta-tag />
2。从输出中删除标签内的换行符而不破坏语义 https://regex101.com/r/GvBc7J/3/
/(\s?\n+\s+|\n)/
最终输出:
<all-tags with-their="attribute" even-if-there="are-more" aa="1">and all the content between the start and end tag</all-tags><meta-tag />
长标题:)
无论如何,我有很多 XML 文件想要即时清理,使用 PHP preg_replace RegEx 输出进行简单即时转换。
现在我无法使更改永久生效,所以我编写了一个 php 函数来遍历文件。
我无法修复的是 RegEx 模式。
https://regex101.com/r/bN5eF4/7
我要匹配:
<all-tags with-their="attribute"
even-if-there="are-more">
and all the content between the start and end tag
even if there
are line breaks
in between them
</all-tags>
我敢打赌这很简单,但我从来没有很好地处理 RegEx...遗憾的是。
已编辑
似乎人们希望我构建一个 SimpleXML 的解析器函数,它遍历 xml 文件并删除换行符?
在同一过程中,我想删除一些元素及其内容,具体取决于它们在属性中的内容。 分析可以这么说。
我认为在使用 Xsltprocessor 处理 xml 文件之前进行换行和分析会是更快的选择?
尝试以下正则表达式:
/(?<=\>)(\r?\n)|(\r?\n)(?=\<\/)/
在这里您要搜索 >
末尾或 </
开头的换行符,并将其替换为空字符串。
在 Regex101
查看演示根据您的示例输入文本,它将删除所有换行符并发出如下内容:
<all-tags with-their="attribute" even-if-there="are-more">and all the content between the start and end tag</all-tags>
我设法用 2 个正则表达式模式做到了。
输入:
<all-tags
with-their="attribute"
even-if-there="are-more"
aa="1">
and all the content between
the start and end tag
</all-tags>
<meta-tag />
1。在打开标签之前和结束标签之后删除换行符 https://regex101.com/r/PPzkWv/2/
/(?<=\>)(\n+)|(\n+)(?=\<)/
输出:
<all-tags
with-their="attribute"
even-if-there="are-more"
aa="1">and all the content between
the start and end tag</all-tags><meta-tag />
2。从输出中删除标签内的换行符而不破坏语义 https://regex101.com/r/GvBc7J/3/
/(\s?\n+\s+|\n)/
最终输出:
<all-tags with-their="attribute" even-if-there="are-more" aa="1">and all the content between the start and end tag</all-tags><meta-tag />