douyong1974 2014-10-28 03:43
浏览 30

写入文件会在行尾添加奇怪的内容

I am working on a program that parses text files uploaded by a user and then saves the parsed XML file on the server. However, when I write the XML file I get some the text



at the end of each line. This text is not in my original text file. I didn't even notice it until I opened the new XML file to verify that it was righting all of the content. Has anyone ran into this before and if so can you tell me if it's due to the way I'm creating and writing my file?

fileUpload.php - These 3 lines occur when the user uploads the file.

$fileName = basename($_FILES['fileaddress']['name']);
$fileContents = file_get_contents($_FILES['fileaddress']['tmp_name']);
$xml = $parser->parseUnformattedText($fileContents);
$parsedFileName = pathinfo($fileName, PATHINFO_FILENAME) . ".xml";
file_put_contents($parsedFileName, $xml);

parser.php

function parseUnformattedText($inputText, $bookName = "")
{
   //create book, clause, text nodes
   $book = new SimpleXmlElement("<book></book>");
   $book->addAttribute("bookName", $bookName);

   $conj = $book->addChild("conj", "X");

   $clause = $book->addChild("clause");

   $trimmedText = $this->trimNewLines($inputText);
   $trimmedText = $this->trimSpaces($inputText);
   $text = $clause->addChild("text", $trimmedText);
   $this->addChapterVerse($text, "", "");

   //make list of pconj's for beginning of file
   $pconjs = $this->getPconjList();

   //convert the xml to string
   $xml = $book->asXml();   

   //combine the list of pconj's and xml string
   $xml = "$pconjs
$xml";
   return $xml;
}

Input text file

1:1 X
it seemed good to me also,
X
having had perfect understanding of all things from the very first
to write you an orderly account, [most] excellent Theophilius
and
1:4
that
you may know the certainty of those things in which you were instructed

1:5 X
There was in the days of Herod, the king of Judea and a certain priest named Zacharias
X
his wife[was] of the daughters of Aaron
and
her name [was] Elizabeth.
1:8 So
it was,
that
while he was serving as priest 1:9 before God in the order of his division,
1:10 and
the whole multitude of the people was praying outside at the hour of incense
but
therefore
it was done.
  • 写回答

2条回答 默认 最新

  • douxianji6181 2014-10-28 12:12
    关注

    &#xD; is the ASCII character for which doesn't seem to come out correctly from parseUnformattedText().

    Try $xml = nl2br($parser->parseUnformattedText($fileContents));

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。