PHP获取.txt文件的内容,希望保留输入,制表符和空格


PHP, getting content of a .txt file, want to keep enters, tabs and spaces

我正在获取保存在文本文件中的网页内容:PHP Simple HTML DOM Parser。将其保存到文本文件中。接下来,我想获取介于 pre 和/pre 之间的内容。(带 HTML 括号)

(只有当你好奇时,我只想要一个音乐标签的信息,fe:http://www.guitaretab.com/p/pink-floyd/14683.html)

我这样做:

    $first_step = explode( '<pre>' , $page );
    $second_step = explode("</pre>" , $first_step[1] ); 
    echo $second_step[0];

我遇到的问题是,在我的.txt文件中,文本格式很好,歌词上方的和弦带有空格/制表符,输入如下:

 G                        C
 Heavy hung the canopy of blue
 G                        C
 Shade my eyes and I can see you

回声线导致所有东西都站在 1 行中而没有进入等。我不知道我能做些什么来防止这种情况。谢谢你,如果你能指出我正确的方向。

你可以使用 php pregreplace

preg_replace(/'s+/g, ' ',$string);

这将在没有pre标签的情况下回显

$pos = strpos($page, '<pre>')+5;
$length = strpos($page, '</pre>') - $pos;
echo strip_tags(substr($page, $pos, $length));

这将与预标签相呼应

$pos = strpos($page, '<pre>')+5;
$length = strpos($page, '</pre>') - $pos;
echo '<pre>' . strip_tags(substr($page, $pos, $length) . '<pre>';