我试图弄清楚如何删除文本文件之间和包括在文本文件中的所有内容。
到目前为止,我有这个从文件中删除整个 urlset。
$myFile = "/here/it/is/sitemap.xml";
$stringdata = file_get_contents($myFile);
$stringdata = preg_replace('#(<urlset.*?>).*?(</urlset>)#', '$1$2', $stringdata);
$stringdata = str_replace('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"></urlset>', '', $stringdata);
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $stringdata);
fclose($fh);
return;
问题是我在文本文件中有几个 urlset,但我只希望删除其中一个 urlset,它与某个变量匹配,所以像这样(不起作用):
$stringdata = preg_replace('#(<urlset.*?>).*? . $thisvariableisintheurlset . (</urlset>)#', '$1$2', $stringdata);
有谁知道我需要添加什么来删除目标 urlset,或者如果这看起来是解决问题的不好方法,甚至是更好的方法?
谢谢