By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,077 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,077 IT Pros & Developers. It's quick & easy.

Problems with str_replace

P: n/a
Hi,

I'm trying to automatically update an existing rss podcast file using
php and the str_replace function.
Every time I add a media file, it should read the .rss file into a
string, take the closing </channel> and </rss> tags out of it, and add
the new rss file info to the end of the existing string, writing it
back to the rss file. However, str_replace will not replace the
</channel> and </rss> tags with ' '. I'm new to php, any help would be
appreciated.

Start Code:
----------------
$xmlFile = "/home2/kingshar/public_html/podcast_test.rss";
$xmlReadHandle = fopen($xmlFile, 'r') or die("Cannot open podcast
file");
$xmlFileContents = fread($xmlReadHandle, filesize($xmlFile));
fclose($xmlFile);
//Search and delete closing rss tag
str_replace('</rss>', '', $xmlFileContents);
//Search and delete closing channel tag
str_replace('</channel>', '', $xmlFileContents);

//Write existing data and new data to xml file

$newxmlFileContents = $xmlFileContents.
"\n<title>".$stitle."</title>\n<itunes:author>Author's
Name</itunes:author>\n<description>".$scripture."</description>\n<pubDate>".$store_sdate."</pubDate>\n<gui
isPermaLink=false>".$audioFile."</guid>\n<enclosure url=".$audioFile .
$audioSize ."type=audio/mpeg
/>\n<itunes:explicit>clean</itunes:explicit>\n</item>\n\n</channel>\n</rss>";
$xmlWriteHandle = fopen($xmlFile, 'w') or die("Cannot open podcast
file");
fwrite($xmlWriteHandle, $newxmlFileContents);
fclose($xmlFile);

Apr 14 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hello

Unless I'm missing something, I think the problem is that you are not
capturing the output of str_replace(). It does not modify the string
in-place; it returns the modified version of the string. So, instead
of:

str_replace('</rss>', '', $xmlFileContents);
str_replace('</channel>', '', $xmlFileContents);

do:

$xmlFileContents = str_replace('</rss>', '', $xmlFileContents);
$xmlFileContents = str_replace('</channel>', '', $xmlFileContents);

HTH

-Kurt

ma**********@gmail.com wrote:
Hi,

I'm trying to automatically update an existing rss podcast file using
php and the str_replace function.
Every time I add a media file, it should read the .rss file into a
string, take the closing </channel> and </rss> tags out of it, and add
the new rss file info to the end of the existing string, writing it
back to the rss file. However, str_replace will not replace the
</channel> and </rss> tags with ' '. I'm new to php, any help would be
appreciated.

Start Code:
----------------
$xmlFile = "/home2/kingshar/public_html/podcast_test.rss";
$xmlReadHandle = fopen($xmlFile, 'r') or die("Cannot open podcast
file");
$xmlFileContents = fread($xmlReadHandle, filesize($xmlFile));
fclose($xmlFile);
//Search and delete closing rss tag
str_replace('</rss>', '', $xmlFileContents);
//Search and delete closing channel tag
str_replace('</channel>', '', $xmlFileContents);


Apr 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.