<string id="10175" text="There is no available named position in the current document!
\nPlease select another option to continue."/>xml中有一条是这样时$file=file_get_contents('demo.xml');    $arr=array('text="There is no available named position in the current document!
\nPlease select another option to continue."' => 'text="[2697]"');
    $data=str_replace(array_keys($arr),$arr,$file);这样替不了,为什么

解决方案 »

  1.   

    检查你的xml和php文件格式是否一致,windows换行是\r\n, unix换行是\n
      

  2.   

    $file=file_get_contents('demo.xml');    $arr=array('text="There is no available named position in the current document!\r\nPlease select another option to continue."' => 'text="[2697]"');
        $data=str_replace(array_keys($arr),$arr,$file);也还是不行呢
      

  3.   

    $arr=array("text=\"There is no available named position in the current document!
    \nPlease select another option to continue.\"" => 'text="[2697]"');
      

  4.   

    你的demo.xml里面是一个硬换行加上字符串\n而php里面如果想表示硬换行可以直接对字符串硬换行,或者用双引号的\n \r\n表示即"\n" or "\r\n" 要想表示\n字符串应用'\n'
    也就是说这样一个字符串
    abc
    def
    可以用"abc\ndef"(假设unix格式)表示
    那么abc
    \ndef应该是"abc\n".'\ndef'前一个\n在双引号中表示换行,后一个\n在单引号中就是\n或者这样写'abc
    \ndef'直接对字符串换行,当然不知道你的php和xml文件格式是否一致
      

  5.   

    人家xml里面直接读出的字符串'\n'不是"\n"
      

  6.   

    还是不行 什么叫php和xml文件格式是否一致?
    而且替换时还发现有这样的问题<string id="61956" text="-ps &lt;Password&gt; Input the passoword of this document.\n"/>这样的也没办法替换,取出来的字符串中间没那么大的空格,是由于全角问题么,怎么解决
    帮帮忙,急!在线等~
      

  7.   

    你应该用正则
    $data = preg_replace('|text="[^"]+"|isU', 'newtext', $data);
      

  8.   

    我现在想把xml中的text="..."里的\r\n都去掉 要怎么写正则表达示呢 
    给个答复,谢谢了~~
      

  9.   

    你说的是硬换行还是字符串
    $data = preg_replace('|(text="[^"]+")|isUe', "str_replace('\\r\\n', '', '\\1')", $data);
    类似这样
      

  10.   

    硬换行试试
    $data = preg_replace('|(text="[^"]+")|isUe', "str_replace(array(\"\\n\", \"\\r\"), '', '\\1')", $data);
      

  11.   

    $data = preg_replace('|(text="[^"]+")|isUe', "str_replace(array(\"\\n\", \"\\r\"), '', '\\1')", $data); 我测试过不会去掉'\n'的
      

  12.   

    嗯 可以了 可是text=\"There is no available named position in the current document!\nPlease select another option to continue.\"
    能不能不要头尾的‘\’呢
    偶不会用正则  所以不知道怎么改 
    学习了~~
      

  13.   

    sorry,没细看
    $data = preg_replace('|text="([^"]+)"|isUe', "'text=\"'.str_replace(array(\"\\n\", \"\\r\"), '', '\\1').'\"'", $data);
      

  14.   

    没事,太谢谢了~~
    那我这样写$data = preg_replace('|text="([^"]+)"|isUe', "'text=\"'.str_replace(array(\"\\n\", \"\\r\",\" \"), array('','',' '), '\\1').'\"'", $file);是不是也能把全角空格一起换了
      

  15.   

    我刚试完,可以呢
    能问下
    |text="([^"]+)"|isUe 
    是什么意思呢
      

  16.   

    正则表达式
    http://php.net/manual/en/book.pcre.php