I'm using imap from php and retrieving a message. The contents can either be html or plain text. When html is available, I resort to using the html contents for displaying. However, if html text is not available, than I must use the plain text for rendering messages to the user.
My question is, when I display the plain text, it comes as one jarbled paragraph. I'd like to figure out how to format the plain text into a viewable fashion. This could just be a multipart issue, and the parts together form one jarbled paragraph. If so, what should I do? Erase the parts that are not the most recent? Is there a way to format the plain text regalrdess
Thanks
You can enclose the text in <pre> tags to preserve the formatting
You can also use nl2br to insert <br /> before all new lines
Related
Any ideas why formatted text from DB, when echo-ed out in php loses its formatting, i.e. no new lines? Thanks!
Use nl2br().
New lines are ignored by browser. That's why you see all text without line breaks. nl2br() converts new lines to <br /> tags that are displayed as new lines in browsers.
If you want to display your text in <textarea>, you don't need to convert all new lines to <br />. Anyway, if you do it... you will see "<br />"s as text in new lines places.
Because there are no html tags for formatting!
Try the nl2br function.
You could try add nl2br() function...
something like this: echo nl2br($your_text_variable);
It should work ;-)
The reason
This is the default behavior for all user agents. If you look at the page source, you'll see that your text has the same formatting like the one in the database (or textarea).
The reason of your confusion is probably that you once see the text in the <textarea> tag, which displays preformatted text, does not interpret the tags, and in the other case the text is interpreted (whitespace is not important in this case).
The browsers don't display new lines, unless specifically asked for - using <br> tag or any block level tags.
No tags == no new lines.
The fix
If you store preformatted text in the database,
you should wrap the output in the <pre> tag.
You may want to convert the formatting characters to the HTML tags you need using set of functions like nl2br, str_replace etc.
You may also correct your structure to store the HTML in the database instead of just plain text (however markup looks like a better solution).
See similar question:
How do I keep whitespace formatting using PHP/HTML?
The difference between the two images you show is that one has the text in a <textarea></textarea> and the other does not ... if you want 1:1: <textarea><?php echo $yourVariable;?></textarea>
It does output what you say to output. If the text is pre-formatted, put it inside the HTML <pre></pre> tag in your output script.
This should be helpful in answering.
How do I keep whitespace formatting using PHP/HTML?enter link description here
Set up a string preprocessing code for both input to database and output to display page
I am using wysiwyg editor to edit my contents in front end. So at the time of saving data in database the contents are saved in html structure format. At the content listing page I am stripping out the text for the option of read more using Str::words. It works fine when the contents are plain text, but how to deal with html contents? For example, if the content is this, and I am limiting 3 words:-
<b>This is a content of the blog</b>
So above function will clip out before closing the bold tag, effecting the entire html document. Any solutions?
Maybe you want to use strip_tags on your content before Str::words. This will remove any HTML tags from you content.
I'm currently using nl2br() when reading text from a database. Unfortunately, if I wrap some text within with or the text doesn't bold it and shows the code still. How do I allow for some html to go through and be rendered correctly?
I'm currently getting this outputted from the database...
<b>Blah blah text</b>
when I'd like it to show bold instead of the tags still existing.
For ex:
In the database I have a dummy post to test if the database works.
The database read...
Hello and welcome to the forums!
<b>Test</b>
That information is being read back through nl2br() as..
Hello and welcome to the forums!
<b>Test</b>
I'd like test to be bolded.
It is not possible to format text in text area. You may try using div and then ContentEditable
Here are references
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.contentEditable
https://developer.mozilla.org/en/docs/Rich-Text_Editing_in_Mozilla
You can't. HTML <textarea> elements can only contain plain text.
You will need to use a Javascript rich text editor component such as TinyMCE or CKEditor.
You've gotta be talking about this: http://php.net/manual/tr/function.html-entity-decode.php
Basically, I want to to something similar as to what mybb does in php, where they will output code between [code][/code] tags as highlighted text. Obviously I will need some regular expressions, but what's the key to outputting all php code as plain text, especially after stored in a db.
Edit: not just php code but all code as plain text
IMO, the easiest way would be to:
1) Read your text into a string
2) Optionally, run htmlspecialchars()
3a) echo() the string
3b) be sure to add <pre> and </pre> tags before and after the string
Any ideas why formatted text from DB, when echo-ed out in php loses its formatting, i.e. no new lines? Thanks!
Use nl2br().
New lines are ignored by browser. That's why you see all text without line breaks. nl2br() converts new lines to <br /> tags that are displayed as new lines in browsers.
If you want to display your text in <textarea>, you don't need to convert all new lines to <br />. Anyway, if you do it... you will see "<br />"s as text in new lines places.
Because there are no html tags for formatting!
Try the nl2br function.
You could try add nl2br() function...
something like this: echo nl2br($your_text_variable);
It should work ;-)
The reason
This is the default behavior for all user agents. If you look at the page source, you'll see that your text has the same formatting like the one in the database (or textarea).
The reason of your confusion is probably that you once see the text in the <textarea> tag, which displays preformatted text, does not interpret the tags, and in the other case the text is interpreted (whitespace is not important in this case).
The browsers don't display new lines, unless specifically asked for - using <br> tag or any block level tags.
No tags == no new lines.
The fix
If you store preformatted text in the database,
you should wrap the output in the <pre> tag.
You may want to convert the formatting characters to the HTML tags you need using set of functions like nl2br, str_replace etc.
You may also correct your structure to store the HTML in the database instead of just plain text (however markup looks like a better solution).
See similar question:
How do I keep whitespace formatting using PHP/HTML?
The difference between the two images you show is that one has the text in a <textarea></textarea> and the other does not ... if you want 1:1: <textarea><?php echo $yourVariable;?></textarea>
It does output what you say to output. If the text is pre-formatted, put it inside the HTML <pre></pre> tag in your output script.
This should be helpful in answering.
How do I keep whitespace formatting using PHP/HTML?enter link description here
Set up a string preprocessing code for both input to database and output to display page