【问题标题】:Simplehtmldom parsing <pre> but got errorSimplehtmldom 解析 <pre> 但出现错误
【发布时间】:2026-01-23 10:55:01
【问题描述】:

我在网站目标中得到了这种代码

http://lufy.byethost6.com/tes.html

并使用此代码对其进行解析

<?php 
include('simple_html_dom.php');
$html = new simple_html_dom();
$html = file_get_html('http://lufy.byethost6.com/tes.html');
$ret = $html->find('pre',0);
echo $ret;
?>

当我看到结果时,它变成了这样的一行

  title goes here    Intro : Gm Cm           Gm Cm       Gm                                  Cm  lyrics, lyrics, lyrics, lyrics, lyrics, lyrics,     Gm                   Cm  lyrics, lyrics, lyrics, lyrics, lyrics, lyrics 

我希望结果与目标网站相同

title goes here

Intro : Gm Cm 
        Gm Cm 


Gm                                  Cm
lyrics, lyrics, lyrics, lyrics, lyrics, lyrics, 
  Gm                   Cm
lyrics, lyrics, lyrics, lyrics, lyrics, lyrics

有可能吗?

【问题讨论】:

  • 查看生成页面的源码时,内容周围是否有pre标签?
  • 是的,在解析结果时仍然存在预代码

标签: php simple-html-dom


【解决方案1】:

我认为你需要使用

echo $ret->outertext;

http://simplehtmldom.sourceforge.net/manual_api.htm

【讨论】:

    【解决方案2】:

    您需要将标志$stripRN 设置为false,因为默认情况下,换行符 标签被剥离。改为:

    $html = file_get_html('http://lufy.byethost6.com/tes.html', $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=false, $defaultBRText=DEFAULT_BR_TEXT);
    

    【讨论】:

    • 感谢它的工作 :) 我在哪里可以找到关于那个 $stripRN 伴侣的参考资料?我想读它