如何将str_replace与数组一起用于多个项目?

时间:2016-02-09 07:19:08

标签: php arrays xml str-replace

我有这段代码:

libxml_use_internal_errors(TRUE);
    $dom = new DOMDocument;
    $dom->Load('/home/dom/public_html/cache/feed.xml');
    $xmlor = '/home/dom/public_html/cache/feed.xml';

    // open file and prepare mods
    $fh = fopen($xmlor, 'r+');
    $data = fread($fh, filesize($xmlor));
    $dmca_claim_jpg = array( 'baduser_.jpg','user78.jpg' );
    $dmca_claim_link = array( 'mydomain.com/baduser_','mydomain.com/user78' );
    echo "Opening local XML for edit..." . PHP_EOL;
    $new_data = str_replace("extdomain.com", "mydomain.com", $data);
    $new_data2 = str_replace($dmca_claim_jpg, "DMCA.jpg", $data);
    $new_data3 = str_replace($dmca_claim_link, "#", $data);
    fclose($fh);

    // run mods
    $fh = fopen($xmlor, 'r+');
    fwrite($fh, $new_data);
    fwrite($fh, $new_data2);
    fwrite($fh, $new_data3);
    echo "Updated feed URL and DMCA claims in local XML..." . PHP_EOL;
    fclose($fh);

执行时不会出现任何错误,但在将$new_data2$new_data3写入xml文件时,通过删除前两行(奇怪)来混淆xml文件。

只能写$new_data ...

我认为它与$dmca_claim_jpg/link数组有关。

1 个答案:

答案 0 :(得分:0)

使用SimpleXML或DOMDocument解析XML更干净,并且您有一种标准的OOP方式来访问节点