Preserve Line Breaks - Simple HTML DOM Parser

后端 未结 5 2049
忘掉有多难
忘掉有多难 2021-02-01 18:21

When using PHP Simple HTML DOM Parser, is it normal that line breaks
tags are stripped out?

5条回答
  •  被撕碎了的回忆
    2021-02-01 18:39

    If you were passing by here wondering if you can do the same thing in DomDocument then I'm please to say you can! - but it's a bit dirty :(

    I had a snippet of code I wanted to tidy but retain the exact line breaks it contained (\n). This is what I did....

    // NOTE: If you're HTML isn't a full HTML document then expect DomDocument to
    // start creating its own DOCTYPE, head and body tags.
    
    
    // Convert \n into a pretend tag
    $myContent = preg_replace("/[\n]/","",$myContent);
    
    // Do your DOM stuff...
    $dom = new DOMDocument;
    $dom->loadHTML($myContent);
    $dom->formatOutput = true;
    
    $myContent = $dom->saveHTML();
    
    // Remove the \n's that DOMDocument put in itself
    $myContent = preg_replace("/[\n]/","",$myContent);
    
    // Put my own \n's back
    $myContent = preg_replace("//i","\n",$myContent);
    

    It's important to note that I know, without a shadow of a doubt that my input contained only \n. You may want your own variations if \r\n or \t needs to be accounted for. eg slash.T or slash.RN etc

提交回复
热议问题