<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);这样替不了,为什么
$data=str_replace(array_keys($arr),$arr,$file);也还是不行呢
\nPlease select another option to continue.\"" => 'text="[2697]"');
也就是说这样一个字符串
abc
def
可以用"abc\ndef"(假设unix格式)表示
那么abc
\ndef应该是"abc\n".'\ndef'前一个\n在双引号中表示换行,后一个\n在单引号中就是\n或者这样写'abc
\ndef'直接对字符串换行,当然不知道你的php和xml文件格式是否一致
而且替换时还发现有这样的问题<string id="61956" text="-ps <Password> Input the passoword of this document.\n"/>这样的也没办法替换,取出来的字符串中间没那么大的空格,是由于全角问题么,怎么解决
帮帮忙,急!在线等~
$data = preg_replace('|text="[^"]+"|isU', 'newtext', $data);
给个答复,谢谢了~~
$data = preg_replace('|(text="[^"]+")|isUe', "str_replace('\\r\\n', '', '\\1')", $data);
类似这样
$data = preg_replace('|(text="[^"]+")|isUe', "str_replace(array(\"\\n\", \"\\r\"), '', '\\1')", $data);
能不能不要头尾的‘\’呢
偶不会用正则 所以不知道怎么改
学习了~~
$data = preg_replace('|text="([^"]+)"|isUe', "'text=\"'.str_replace(array(\"\\n\", \"\\r\"), '', '\\1').'\"'", $data);
那我这样写$data = preg_replace('|text="([^"]+)"|isUe', "'text=\"'.str_replace(array(\"\\n\", \"\\r\",\" \"), array('','',' '), '\\1').'\"'", $file);是不是也能把全角空格一起换了
能问下
|text="([^"]+)"|isUe
是什么意思呢
http://php.net/manual/en/book.pcre.php