Jump to content

Search a File and Delete Un-Needed Parts


dylandcor

Recommended Posts

I have a script that creates an xml podcast file on the fly after the audio is uploaded.  The administrator enters some information and then it is done.  The file is set up to mimic the iTunes specifications.

 

When the file is created, I used file_get_contents to add the new audio to the old ones.  Since the newest is at the top, I made the php file create the channel information each time as well.  I was wondering if there was some way to delete the old channel information so there aren't two copies.

 

I tried preg_replace, but I keep getting this error:

Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 0

 

Hopefully this makes sense.

 

What I tried in the php file:

$myFile = "rss/new_file.xml";
$myFile2 = "rss/new_file.xml.bak";
copy ($myFile, $myFile2);
$contents = file_get_contents ($myFile);
$patterns = "<?xml version=\"1.0\"?>";
#$patterns = "<rss xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\" version=\"2.0\">";
#$patterns = "	<channel>";
#$patterns = "		<title>title</title>";
#$patterns = "		<link>url</link>";
#$patterns = "		<description>desc</description>";
#$patterns = "		<itunes:author>url</itunes:author>";
#$patterns = "		<itunes:image href=\"url\"/>";
#$patterns = "		<itunes:category text=\"None\">";
#$patterns = "		<itunes:category text=\"test\"/>";
#$patterns = "		</itunes:category>";
#$patterns = "		<copyright>2009</copyright>";
#$patterns = "		<lastBuildDate>Sun, ".$lastbuilddate2." 00:00:00 -0500</lastBuildDate>";
#$patterns = "		<itunes:explicit>No</itunes:explicit>";

$replacements = ' ';
echo preg_replace($patterns, $replacements, $contents);

 

This is what I get in the podcast file. I wanted to remove the second channel info before I add the new entry:

<?xml version="1.0"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
	<title>title</title>
	<link>url</link>
	<description>desc</description>
	<itunes:author>author</itunes:author>
	<itunes:image href="url"/>
	<itunes:category text="None">
	<itunes:category text="test"/>
	</itunes:category>
	<copyright>2009</copyright>
	<lastBuildDate>Sun, 12 Apr 2009 00:00:00 -0500</lastBuildDate>
	<itunes:explicit>No</itunes:explicit>





<item>
	<title>title</title>
	<pubDate>Sun, 26 April 2009 00:00:00 -0500</pubDate>
	<enclosure url="url" length="xxxxxxxx" type="audio/mpeg"/>
	<guid>url</guid>
	<description>desc</description>
	<itunes:duration>42:03</itunes:duration>
	<itunes:keywords>keywords</itunes:keywords>
	</item>

<?xml version="1.0"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
	<title>title</title>
	<link>url</link>
	<description>desc</description>
	<itunes:author>author</itunes:author>
	<itunes:image href="url"/>
	<itunes:category text="None">
	<itunes:category text="test"/>
	</itunes:category>
	<copyright>2009</copyright>
	<lastBuildDate>Sun, 12 Apr 2009 00:00:00 -0500</lastBuildDate>
	<itunes:explicit>No</itunes:explicit>





<item>
	<title>title</title>
	<pubDate>Sun, 26 April 2009 00:00:00 -0500</pubDate>
	<enclosure url="url" length="xxxxxxxx" type="audio/mpeg"/>
	<guid>url</guid>
	<description>desc</description>
	<itunes:duration>42:03</itunes:duration>
	<itunes:keywords>keywords</itunes:keywords>
	</item>
<item>
	<title>title</title>
	<pubDate>Sun, 26 April 2009 00:00:00 -0500</pubDate>
	<enclosure url="url" length="xxxxxxxx" type="audio/mpeg"/>
	<guid>url</guid>
	<description>desc</description>
	<itunes:duration>42:03</itunes:duration>
	<itunes:keywords>keywords</itunes:keywords>
	</item>.......

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.