Outputting correctly a very long textarea value in PHP - php

I have a textarea with input like this:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
I want an output with line breaks like this:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
How can I do that?
In my previous question, someone suggested wordwrap, which would be useful if the line had spaces.

Yes, use wordwrap...
$text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
$newtext = wordwrap($text, 10, "<br />", true);
echo $newtext;

I don't know if I understand the question correctly, but HTML textarea field also has the wrap attribute (values "soft", "hard", or "off"). Hard wraps the words inside the text box and places line breaks at the end of each line so that when the form is submitted it appears exactly as it does in the text box.
So:
<textarea cols="30" rows="5" wrap="hard">text..</textarea>

you could insert a "<br /> at the index, where the break should occur - only for the output.
textbox.value = HandleLineBreaks(line, index);

Related

HTML textarea transferred to echo in PHP with line breaks as per user input

My textarea in HTML:
Description<br><textarea name="description" rows="5" cols="27"></textarea>
Within my PHP file I have:
$desc = $_POST['description'];
echo $desc . "<br>"; // needs line breaks as per user input in textarea.
The <br> I have used within my echo is simply there to place some room before and after the other content being entered when the form is submitted.
The text entered into the description box will show up when the form is submitted, however it is on one line and not broken up as shown in the textarea when the enter key is pressed.
How can I include the breaks from the description box into the PHP echo?
EDIT:
I used...
echo "<pre>" . $desc . "<br>";
within my PHP file, works well. Thank you!
Try Using:
echo $desc = nl2br($_POST['description']) . "<br>";
The manual for nl2br. Can be found in the link below
http://php.net/manual/en/function.nl2br.php
You can place the text inside a <pre> element, or any element with the CSS attribute:
white-space: pre;
And the white space, as well as the raw text, will be preserved (i.e. you don't need to modify the value you have been supplied).
Just be careful what you allow to be injected into your HTML, as all user input is unsafe and provides an opportunity for a number of HTML injection attacks.
.output {
white-space: pre;
}
<div class="output">This is sample output.
And it has line breaks.
So it is three lines.</div>
echo $desc = nl2br($_POST['description']) . "<br>";
is a simple, one line solution :)

Echo with auto \n from data base

Hey I have on my database a text with 800 chracters when I echo it out it display in just one line getting too big and bad for read,
echo mysql_result(mysql_query("SELECT `message` FROM `pm` WHERE `pm_id` = '1'"));
How do I put or \n automatic on the end of every line because there is differents datas inserted in the database and do manualy for every one is hard.
edit:
I want to meke it like: one \n after 100 characters or something like that.
Use the wordwrap function: http://www.php.net/manual/en/function.wordwrap.php
echo wordwrap($data, 100, "\n"); // Or use "<br/>" in html
There also is a 4. parameter. If you data does not contain any spaces, wordwrap can not break between words. If you set the 4. parameter to true, wordwrap will cut no matter if within a word or not.
// cut after 100, no matter if inside a word.
echo wordwrap($data, 100, "\n", true);
put it in a div, and set div width. That will solve your problem. Like this:
#CSS
.sample{
width:50px;
}
and here's some sample html
<div class="sample">
#your results from database here
</div>

Formatting line breaks in a <textarea>

I am trying to PHP data that is posted from one place to another textarea.
Here is the case:
after doSearchTweet.php shows the tweets that are retrieved using twitter API, user can checked the tweets they want using a form.
The form will post the selected tweets a textarea input.
Here is the problem, I cannot style the selected tweets in the textarea.
For example: Username - Jakob Tweet - I need help in styling php!
I select this tweet and the data is post into the textarea, it shows this
JakobI need help in styling php!
instead of
Jakob
I need help in styling php!
I tried using normal html tags like <br/> n/ in the textarea, instead of applying the html tags, it will appear as normal text like this
Jakob< b r >I need help in styling php!
here is my code
<tr><td><label for="content"><span class="postBigFont">Content: </span></label></td>
<td colspan="4"><textarea cols="60" rows="15" name="content">
<?php
$user = $_POST['username'];
$content = $_POST['content'];
$add = $_POST['add'];
foreach ($add as $i){
echo $user[$i];
echo $content[$i];
}?>
</textarea></td></tr>
Replace "<br>" with "\n" in your textarea output.
Perhaps nl2br($text); is the answer
$content = str_replace("\r\n", "<br />", $content);

strip <br>(n12br) get from database field

i have a textarea value which its value derived from a field(nl2br)
how to strip off "< br/>", so that when i want to edit this field, the "< br />" will not be appeared?
//$data["Content"] is the field that has <br/> tags inside
$content = $data["Content"];
//when want to edit, want to strip the <br/> tag
<td><textarea name="content" rows="10" style="width:300px;"><?=$content?></textarea></td>
i know it should be using strip_tags() function but not sure the real way to do it
any help would be appreciated
If you wanna use strip_tags, then it would just be:
$content = strip_tags($data["Content"]);
i would be using str_replace the following will replace <br/> with newline
$content = str_replace('<br/>','\n',$data['Content']);
or if you don't want the newline
$content = str_replace('<br/>','',$data['Content']);
edit
an example
$my_br = 'hello<br/> world';
$content = str_replace('<br/>','',$my_br);
echo $content;
Output: hello world

Preserve Line Breaks From TextArea

I'm using a textarea to enable users to input comments. However, if the users enters new lines, the new lines don't appear when they are outputted. Is there any way to make the line breaks stay.
Any idea how do preserve the line breaks?
Two solutions for this:
PHP function nl2br():
e.g.,
echo nl2br("This\r\nis\n\ra\nstring\r");
// will output
This<br />
is<br />
a<br />
string<br />
Wrap the input in <pre></pre> tags.
See: W3C Wiki - HTML/Elements/pre
Here is what I use
$textToOutput = nl2br(htmlentities($text, ENT_QUOTES, 'UTF-8'));
$text is the text that needs to be displayed
$textToOutput is the returned text from nl2br and htmlentities so it can be safety displayed in the html context.
ENT_QUOTES will convert both double and single quotes, so you'll have no trouble with those.
Got my own answer: Using this function from the data from the textarea solves the problem:
function mynl2br($text) {
return strtr($text, array("\r\n" => '<br />', "\r" => '<br />', "\n" => '<br />'));
}
More here: http://php.net/nl2br
i am using this two method steps for preserve same text which is in textarea to store in mysql
and at a getting time i can also simply displaying plain text.....
step 1:
$status=$_POST['status'];<br/>
$textToStore = nl2br(htmlentities($status, ENT_QUOTES, 'UTF-8'));
In query enter $textToStore....
step 2:
write code for select query...and direct echo values....
It works
This works:
function getBreakText($t) {
return strtr($t, array('\\r\\n' => '<br>', '\\r' => '<br>', '\\n' => '<br>'));
}
function breakit($t) {
return nl2br(htmlentities($t, ENT_QUOTES, 'UTF-8'));
}
this may help you
pass the textarea wal
why make is sooooo hard people when it can be soooo easy :)
//here is the pull from the form
$your_form_text = $_POST['your_form_text'];
//line 1 fixes the line breaks - line 2 the slashes
$your_form_text = nl2br($your_form_text);
$your_form_text = stripslashes($your_form_text);
//email away
$message = "Comments: $your_form_text";
mail("destination_email#whatever.com", "Website Form Submission", $message, $headers);
you will obviously need headers and likely have more fields, but this is your textarea take care of

Categories