将 <br> 替换为 sed 中的 \n 或 XML 文件中的 PHP

Replace &lt;br&gt; with \n in sed or PHP in XML file

我的 <br> 标签在导出 XML 文件后变成了 &lt;br&gt;。我想用 \n

替换每个 &lt;br&gt;

这是一个字符串示例,其中出现了这种情况:

to download the ~ &lt;br&gt;to mark on the ~ &lt;br&gt;to attach/enclose the ~

我想要:

to download the ~ \nto mark on the ~ \nto attach/enclose the ~

我在 PHP 做了一些研究后尝试了以下方法:

$filexml='myfile.xml';

$oldxml = simplexml_load_file($filexml);

$xml = simplexml_load_string ( str_replace( "&lt;br&gt;", "\n", $oldxml->asXml() ) );
$xml->asXml('updated.xml');

没有字符串被替换,updated.xml 与 myfile.xml 的内容相同。

我尝试使用 sed 替换字符串:

sed -i "s/&lt;br&gt;/\n/g" myfile.xml

同样没有字符串被替换,myfile.xml保持原样。

如果有人有答案,我将不胜感激!

您可以通过 Stream EDitor 尝试以下命令吗:

sed -i 's/original/new/g' file.txt

解释:

  • sed = 流编辑器
  • -i = in-place(为了存回原文件)
  • s = 替代命令
  • 原始 = 要替换的表达式或单词
  • new = 要替换的文本
  • g = 全局(替换所有匹配,而不仅仅是第一次出现
  • file.txt = 文件名

编辑 1

此解决方法为我提供了您要求的结果,请测试它:

$str = "&lt;br&gt;to mark on the ~ &lt;br&gt;to attach/enclose the";
$str1=str_replace( "&lt;br&gt;", "\n", $str);
str_replace ("<br>", "\n", $str1);
echo $str1;

请问您使用哪个应用程序提取XML文件?

我复制了导出的 XML 文件的内容,刚刚创建了一个新文件并对该文件使用了 sed 命令。这不知何故奏效了,但如果有人知道为什么我原来的 XML 如此顽固,我仍然会很感激!