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
Related
I have an string that contains the body of an e-mail on html. So I need to remove one of the body lines. I made a dirty trick that work, but i guess I can found a more efficient way.
For instance I have:
<p style="background:white">
<span style="font-size:10.0pt;font-family:"Trebuchet MS","sans-serif"">
Coût element : 43.19 €.
<u></u><u></u>
</span>
</p>
And I want to remove the entire line.
I did a function that removes just the price, and stops when it fount the first letter of the next line (in that case "L"). The function:
function clear_french($body){
$value = utf8_encode(quoted_printable_decode('element'));
$ini = strpos($body,$value);
$i = 0;
if($ini === false){}
else{
while ( $body[$ini+strlen($value)+$i] != 'L' ) {
//echo $body[$ini+strlen($value)+$i];
$body[$ini+strlen($value)+$i]="";
$i++;
}
if ($body[$ini+strlen($value)+$i] == 'L'){
$body[$ini+strlen($value)+$i-1]=" ";
}
}
return $body;
}
But I don't know if there are any efficient way to do that more clean and fast. And I dunno if it should be more easy to work with text plain in $body, if it's easier how can I do it?
Note: I want to delete the cost, because I have to resent the e-mails without it.
Hope anyone helps me!
Thanks in advance.
A dirty little trick I use when I want to have placeholders and conditional parts in my HTML emails is using HTML comments. If you have control over the HTML that's sent, you can have your HTML emails set up like this :
<p style="background:white">
<span style="font-size:10.0pt;font-family:"Trebuchet MS","sans-serif"">
Coût element : <!-- __CONDITION_NAME_START__ -->43.19 €.<!-- __CONDITION_NAME_END__ -->
<u></u><u></u>
</span>
</p>
And then send the email containing the price once. (Price will appear because it is not commented)
Then you can use preg_replace to strip that part :
$newBody = preg_replace('<!-- __CONDITION_NAME_START__ -->(.|\n)*?<!-- __CONDITION_NAME_END__ -->', '', $body);
And send $newBody as your email not containing the price
I finally did it with regex, with something like taht:
$body = preg_replace('#(Translation\s+cost)\s*:\s*\d+(\.\d+)?#u', '$1:', $body);
And it works!
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);
I am doing something like posting function in a local app it's working fine really but it lacks with validation and not to mention the validation I made was a mess. I'm using jQuery oEmbed.
What I wanted is to print the illegal html tag(s) as is and activate/perform(I don't know the right term) the html tags I have allowed.
Any suggestions?
This is the best solution i came up.
First replaced all the < and > for html code then replaced back the allowed tags.
<?php
$original_str = "<html><b>test</b><strong>teste</strong></html>";
$allowed_tags = array("b", "strong");
$sans_tags = str_replace(array("<", ">"), array("<",">"), $original_str);
$regex = sprintf("~<(/)?(%s)>~", implode("|",$allowed_tags));
$with_allowed = preg_replace($regex, "<\\1\\2>", $sans_tags);
echo $with_allowed;
echo "\n";
Result:
guax#trantor:~$ php teste.php
<html><b>test</b><strong>teste</strong></html>
I wonder if there's any solution for replacing all at once. But it works.
Hi so i was wondering how do i get a load of string from a mysql database and put them in a input area with a return between each value such as
Hi
My
Name
Is
Joris
Thanks,
Joris
When you're creating a <textarea> in a form, the text between the <textarea> and </textarea> tags is the default text for the text area. You just need to build a pair of these with the string you want between them, like this: <textarea>$string</textarea>. Do the query and the concatenating in PHP using \n for newlines.
You can simply add a carriage return after each string and then output this within the required textarea.
For example:
<?php
$cr = "\n";
$bigString = 'Hello' . $cr . 'there.';
echo '<textarea>' . $bigString . '</textarea>';
?>
i have a textarea with ckeditor customized toolbar which user can only select smiley
now i want users can only use smilys and new line function
so if $comment is my raw text output with html tags and i want only smiley and text with new lines appeared in output not other html tags
sample raw data :
<p>
this is a sample text</p>
<p>
<img alt="angry" src="includes/ckeditor/plugins/smiley/images/angry_smile.gif" title="angry" /></p>
<p>
text for sample</p>
for new line in html :
$comment = nl2br($comment);
but what about showing only smileys ?!
As I answered in your previous question, encode your output with htmlspecialchars and then use nl2br to convert new lines to <br> tags.
To replace text smilies with graphics, you want a series of regular expressions rather than attempting to explode the string and output it in pieces.
The following should give you some idea:
$comment = htmlspecialchars(comment);
$comment = nl2br($comment);
$smilies = array(
'/\b:\)\b/' => '<img src="smile.gif" />', // :)
'/\b:\(\b/' => '<img src="sad.gif" />', // :(
'/\b:p\b/' => '<img src="tongue.gif" />', // :p
);
$comment = preg_replace(array_keys($smilies), $smilies, $comment);
If I understood you correctly, then here is reference, to what you might need: strip_tags
Here is what I came up with:
function smileAndText( $some_text = '' ) {
if( ! empty( $some_text ) ) {
$some_text = nl2br( $some_text );
$some_text = strip_tags( $some_text, '<img><br />' );
}
return $some_text;
}
Look at php strip_tags http://php.net/manual/en/function.strip-tags.php. You could do something like
$comment = strip_tags(nl2br($comment),"<br />, #smiley_tag");
Not sure exactly what you're trying to do with the smileys, but there is a way to configure CK in the javascript config file to use when the enter key is pressed. I'd still think about post-process replacing, but that's a good way to make clean code happen on the front end.