从PHP中的原始文本文件中读取时保留标记

时间:2022-06-18 13:32:24

I have a simple text file. The content has xml tags. The issue with that is some of the elements don't have closing tags.

我有一个简单的文本文件。内容包含xml标签。问题是一些元素没有结束标记。

I am able to read the file in line by line, however the tags are missing when I echo back what was read.

我能够逐行读取文件,但是当我回显读取的内容时,标签丢失了。

Sample File Content.

示例文件内容。

<SOL>
<Name> JJ Evans
<Address> 1010 Good Times Lane
</Sol>

When I read this file, line by line and echo out the output I get:

当我读取这个文件时,逐行并回显我得到的输出:

JJ Evans
1010 Good Times Lane

I need the output to be:

我需要输出为:

<SOL>
<Name> JJ Evans
<Address> 1010 Good Times Lane
</Sol>

And help resolving this would be appreciated.

并且有助于解决这个问题。

1 个解决方案

#1


0  

Try using htmlentities() on each line, it won't let the browser parse the tags. Also if you don't have any particular need to read the file line-by-line, you better stick with file_get_contents(), just call htmlentities(file_get_contents('sample.xml')) and you'll make the job even easier.

尝试在每一行使用htmlentities(),它不会让浏览器解析标签。此外,如果您没有任何特殊需要逐行阅读文件,最好坚持使用file_get_contents(),只需调用htmlentities(file_get_contents('sample.xml')),您就可以更轻松地完成工作。

#1


0  

Try using htmlentities() on each line, it won't let the browser parse the tags. Also if you don't have any particular need to read the file line-by-line, you better stick with file_get_contents(), just call htmlentities(file_get_contents('sample.xml')) and you'll make the job even easier.

尝试在每一行使用htmlentities(),它不会让浏览器解析标签。此外,如果您没有任何特殊需要逐行阅读文件,最好坚持使用file_get_contents(),只需调用htmlentities(file_get_contents('sample.xml')),您就可以更轻松地完成工作。