-1

I am using SIMPLE HTML DOM PARSER PHP.

partial source of document that I want to parse is

<pre>

LINE 1

LINE 2

LINE 3

</pre> 

these code is defined $string and

I wrote those PHP code

$html = str_get_html($string);

$ret = $html->find('pre',0)->plaintext;

echo $ret;

result is

LINE 1 LINE 2 LINE 3

but in web browser those HTML code is showing

LINE 1

LINE 2

LINE 3


web browser's showing is what I want. How can I get same result in PHP?

Taryn East
  • 27,486
  • 9
  • 86
  • 108

4 Answers4

3

You need to set Default Parameter $stripRN to false inside your simple_html_dom.php file

e.g.

$stripRN = false;

change for both functions file_get_html & str_get_html

so this

function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)

becomes

function file_get_html($url, $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, $defaultSpanText=DEFAULT_SPAN_TEXT)

and this

function str_get_html($str, $lowercase=true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)

becomes

function str_get_html($str, $lowercase=true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=false, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)
Community
  • 1
  • 1
2

You want to make line breaks, correct? The following should work:

<?php 
    foreach ($ret as $string) {
        echo "$string\n";
    }
Neil Girardi
  • 4,533
  • 1
  • 28
  • 45
  • thanks, but it doesn't work. each line is not elements of array.
    ~~
    as $ret is one variable. Do I misunderstand?
    – burning web fuel Aug 06 '13 at 03:31
  • Ah I see. I think you want to use preg_split(), or str_split(). If the substrings are six characters in length as in your example you could do this: $array = str_split($ret, 7); Now you can iterate the array, trimming it and adding line breaks. – Neil Girardi Aug 06 '13 at 04:00
  • @NeilGirardi Have a look at my answer above. The "edit" is a lot simpler than my original answer. If you can use it, great. Cheers *"Peace"* – Funk Forty Niner Aug 06 '13 at 05:02
1

EDIT: Reverse effect of original code.

You can use the following to echo text on each line:

Output in browser:

LINE 1

LINE 2

LINE 3

File content:

<pre>
LINE 1

LINE 2

LINE 3
</pre>

PHP code:

<?php

$lines = file('myfile.txt');

foreach ($lines as $line_num => $line) {

    echo ($line);

}

?>

Original answer, misunderstood OP's question yet useful code nonetheless.

You can use the following code to echo text in one line.

File content:

<pre>
LINE 1

LINE 2

LINE 3
</pre>

Output in browser: LINE 1 LINE 2 LINE 3

PHP code:

<?php

$lines_of_file = file("myfile.txt");
//Now $lines_of_file have an array item per each line

$file_content = file_get_contents("myfile.txt");
$file_content_separated_by_spaces = explode(" ", $file_content);

echo $file_content;

?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

I couldn't figure out how to do it with Simple HTML Dom Parser, but I was successful with Zend's Dom Query package. The following code works for maintaining the formatting of the text inside of pre elements:

$dom = new Zend\Dom\Query($html_string);
$results = $dom->execute('pre');
foreach ($results as $result) { // each $result is of type DOMElement
        echo $result->ownerDocument->saveXML($result);
}