我对 PHP 的 file_get_contents 函数有一个奇怪的问题,我目前正在创建一个读取 .md 文件的应用程序。我创建了一个命令,它在一个完美运行的 html 文件中构建 .md 文件。它将所有 html 放在正确的文件中。
但是,如果您正在编写 .md 文件,那么构建 html 以查看每次编辑后的外观会很痛苦。所以我创建了一个可以通过浏览器访问的 php 脚本,它的作用与命令完全相同,但不是将 html 内容放在文件中,而是在屏幕上回显。但是如果你调用这个脚本,它不会读取整个文件,它会切断 html 的最后一部分......
.md 文件
### Test Readme for Category One
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor felis enim, molestie tincidunt erat feugiat a.
Donec quis odio quis quam blandit convallis. Aenean eu lacus risus. Nulla congue non dui eu convallis.
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer ac ante laoreet, placerat diam in,
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor felis enim, molestie tincidunt erat feugiat a.
Donec quis odio quis quam blandit convallis. Aenean eu lacus risus. Nulla congue non dui eu convallis.
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer ac ante laoreet, placerat diam in,
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor felis enim, molestie tincidunt erat feugiat a.
Donec quis odio quis quam blandit convallis. Aenean eu lacus risus. Nulla congue non dui eu convallis.
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer ac ante laoreet, placerat diam in, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor felis enim, molestie tincidunt erat feugiat a.
Donec quis odio quis quam blandit convallis. Aenean eu lacus risus. Nulla congue non dui eu convallis.
输出
<h3>Test Readme for Category One</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor felis enim, molestie tincidunt erat feugiat a.
Donec quis odio quis quam blandit convallis. Aenean eu lacus risus. Nulla congue non dui eu convallis.</p>
<p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer ac ante laoreet, placerat diam in,
Lorem ipsum dolor sit amet, consecte
如您所见,它剪切了 md 文件的一部分。我使用这个库将 md 内容转换为 HTML https://github.com/erusev/parsedown-extra
有没有人想过它可能是什么?
谢谢!
=== 编辑 ===
将该文件的内容存储在本地文件中并执行该本地文件的 file_get_contents。这是它处理的第一个文件,其结果如下:
### Test Readme Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor felis enim, molestie tincidunt erat feugiat a. Donec quis odio quis quam blandit convallis. Aenean eu lacus risus. Nulla congue non dui eu convallis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer ac ante laoreet, placerat diam in, hendrerit augue. Mauris pharetra, dui sed facilisis condimentum, ex lorem lobortis ante, ut ultrices nibh felis sit amet nibh. Pellentesque eget interdum nibh. Aenean interdum felis at tellus bibendum aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus velit augue, interdum id metus id, iaculis porttitor purus.
对于想要查看获取内容的功能的人:
/**
* Parses the md files of the given path
*
* @return string
*/
public function parseItems()
{
$iterate = $this->getRecursiveIterator();
$html = "";
foreach ($iterate as $fullPath => $fileInfo) {
/** @var $fileInfo SplFileInfo */
// Check if it's a directory or a .md file
if ($fileInfo->getExtension() == 'md') {
$fileName = StringHelper::removeMdExtension(StringHelper::removeOrderChars($fileInfo->getFilename()));
$id = "";
for ($depth = $iterate->getDepth() - 1; $depth >= 0; $depth--) {
$id .= StringHelper::removeOrderChars($iterate->getSubIterator($depth)->current()->getFilename())."/";
}
$id .= $fileName;
$headerName = str_replace("_", " ", $fileName);
$html .= "<h1 id='{$id}'>{$headerName}</h1>";
$mdContent = file_get_contents($fileInfo->getRealPath());
print_r($mdContent);die();
$parseDown = new ParsedownExtra();
$html .= $parseDown->text($mdContent);
}
}
return $html;
}