简体   繁体   English

删除两个单词之间的所有错误字符串

[英]delete all bug string between two words

I've got a script which generates text. 我有一个生成文本的脚本。 I need to be strip all repeated blocks of text. 我需要删除所有重复的文本块。 The string is in xml format, so I can use the beginning and ending tags to determine where the strings are. 该字符串为xml格式,因此我可以使用开始和结束标记来确定字符串在哪里。 I've been using substr_replace to remove the unnecessary text... However, this only works if I know how many times said text is going to be present in the string. 我一直在使用substr_replace删除不必要的文本...但是,这仅在我知道在字符串中将出现所述文本多少次时才有效。 Example : 范例:

<container>
  <string1>This is the first string.</string>
  <string2>This is the second string.</string>
  <stuff>This is the important stuff.</stuff>
</container>

That container might appear once, twice six times, seven times, whatever. 该容器可能出现一次,两次,六次,七次,无论如何。 The point is, it's necessary to only have it appear once in the string variable. 关键是,只需要让它在字符串变量中出现一次即可。 Right now this is what I'm doing. 现在这就是我在做什么。

$where_begin = strpos($wsman_output,'<container');
$where_end = strpos($wsman_output,"</container>");
$end_length = strlen("</Envelope>");
$attack = $where_end - $where_begin;
$attack = $attack + $end_length;
$wsman_output = substr_replace($wsman_output,"",$where_begin,$attack);

And I do that for each time the container exists.... However, I just found out that it's not always going to be the same.. Which really messes things up. 我每次容器都存在时执行此操作。...但是,我刚刚发现它并不总是相同的。这确实使事情搞砸了。

Any ideas? 有任何想法吗?

In the end I decided to use the method suggested here . 最后,我决定使用这里建议的方法。

I pulled each block of string I wanted from the variable, then combined them back together in the required order. 我从变量中提取了想要的每个字符串块,然后按所需顺序将它们重新组合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM