Problems with str_replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • marcomputers@gmail.com

    Problems with str_replace

    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.rs s";
    $xmlReadHandle = fopen($xmlFile, 'r') or die("Cannot open podcast
    file");
    $xmlFileContent s = fread($xmlReadH andle, filesize($xmlFi le));
    fclose($xmlFile );


    //Search and delete closing rss tag
    str_replace('</rss>', '', $xmlFileContent s);
    //Search and delete closing channel tag
    str_replace('</channel>', '', $xmlFileContent s);

    //Write existing data and new data to xml file

    $newxmlFileCont ents = $xmlFileContent s.
    "\n<title>".$st itle."</title>\n<itunes :author>Author' s
    Name</itunes:author>\ n<description>" .$scripture."</description>\n< pubDate>".$stor e_sdate."</pubDate>\n<gui
    isPermaLink=fal se>".$audioFile ."</guid>\n<enclosu re url=".$audioFil e .
    $audioSize ."type=audio/mpeg
    />\n<itunes:expl icit>clean</itunes:explicit >\n</item>\n\n</channel>\n</rss>";
    $xmlWriteHandle = fopen($xmlFile, 'w') or die("Cannot open podcast
    file");
    fwrite($xmlWrit eHandle, $newxmlFileCont ents);
    fclose($xmlFile );

  • kurt.milligan@gmail.com

    #2
    Re: Problems with str_replace

    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>', '', $xmlFileContent s);
    str_replace('</channel>', '', $xmlFileContent s);

    do:

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

    HTH

    -Kurt

    marcomputers@gm ail.com wrote:[color=blue]
    > 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.rs s";
    > $xmlReadHandle = fopen($xmlFile, 'r') or die("Cannot open podcast
    > file");
    > $xmlFileContent s = fread($xmlReadH andle, filesize($xmlFi le));
    > fclose($xmlFile );
    >
    >
    > //Search and delete closing rss tag
    > str_replace('</rss>', '', $xmlFileContent s);
    > //Search and delete closing channel tag
    > str_replace('</channel>', '', $xmlFileContent s);
    >[/color]

    Comment

    Working...