-1

I have an HTML page which formats text as show below. I want to generate a text file with the same formatting.

 Text1                                                                 Text2 

    This is the paragraph1

    Text

    01.03.2017
    This is the paragraph 2

Currently, I am using the html2text class but the output I got is different,it shows this below output

    include("html2text/html2text.php");
    $text = convert_html_to_text($my_var);

Generates

Text1 Text2  This is paragraph 1 Text 01.03.2017 This is the paragraph2

So everything in in one line. But I want in the same format as when the HTML document render. How do I achieve this?

Maarten
  • 6,894
  • 7
  • 55
  • 90

2 Answers2

0

Your HTML file does not contain HTML tags, so why would you want to convert it to text ? It is already a text file.

You could add "pre" tags around the $my_var variable, perhaps the library that you use will preserve whitespace inside the tag.

If that does not work you could first replace all double spaces with another string, do the conversion and then replace your string with double spaces again.

Niko Nelissen
  • 120
  • 2
  • 10
  • No it is not a text file it is the html file I showed the browser output here. – Pardeep Kumar May 23 '17 at 08:17
  • Guess he wants to reverse this one https://stackoverflow.com/questions/43996613/how-to-get-text-from-word-file-using-php-accurately. Create a Word file out of HTML – RST May 23 '17 at 08:36
  • NO @RST.this is a completely different problem.do you have any solution for this ? – Pardeep Kumar May 23 '17 at 09:04
0

This is basically a styling problem.

For some background, I recommend you look into Cascading Style Sheets (CSS). Browsers use CSS code to determine how HTML should be rendered to the user.

This means that this problem is only automatically solvable for document formats that support styling. You might look for converters that take HTML + CSS as input, and output your format of choice. For example, look into pandoc.

For plain text, this problem is impossible to solve automatically. You'd need to translate negative space or whitespace to typed spaces, but it is quite hard to determine how much spaces you'd need exactly.

Maarten
  • 6,894
  • 7
  • 55
  • 90