I have the following code:
$message = "Good news! The item# $item_number on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\n
The seller, $bid_user is making this offer.\n
\nItem Title : $title\n
\nAll the best,\n
$bid_user\n
$email\n
";
echo $message;
$htmlContent = $baseClass->email_friend($item_number, $title, $bid_price, $bid_user, $mcat, $message, $email, $VIEWSTATE, $EVENTVALIDATION, $URL);
The problem is that the new line break (\n) is not working.
Try \r\n in place of \n
The difference between \n and \r\n
It should be noted that this is applicable to line returns in emails. For other scenarios, please refer to rokjarc's answer.
I know this is an old question but anyway it might help someone.
I tend to use PHP_EOL for this purposes (due to cross-platform compatibility).
echo "line 1".PHP_EOL."line 2".PHP_EOL;
If you're planning to show the result in a browser then you have to use "<br>".
EDIT: since your exact question is about emails, things are a bit different.
For pure text emails see Brendan Bullen's accepted answer. For HTML emails
you simply use HTML formatting.
Are you building this string using single or double quotes? \r and \n only work with double quotes, as well as embedded variables. For example:
$foo = 'bar';
echo 'Hello \n $foo!';
will output:
Hello \n $foo!
But:
$foo = 'bar';
echo "Hello \n $foo!";
will output:
Hello
bar!
you can use "Line1<br>Line2"
If you output to html or an html e-mail you will need to use <br> or <br /> instead of \n.
If it's just a text e-mail: Are you perhaps using ' instead of "? Although then your values would not be inserted either...
EDIT: Maybe your class for sending emails has an option for HTML emails and then you can use <br />
1) Double-quotes
$output = "Good news! The item# $item_number on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\nThe seller, $bid_user is making this offer.\n\nItem Title : $title\n\nAll the best,\n $bid_user\n$email\n";
If you use double-quotes then \n will work (there will be no newline in browser but see the source code in your browser - the \n characters will be replaced for newlines)
2) Single quotes doesn't have the effect as the double-quotes above:
$output = 'Good news! The item# $item_number on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\nThe seller, $bid_user is making this offer.\n\nItem Title : $title\n\nAll the best,\n $bid_user\n$email\n';
all characters will be printed as is (even variables!)
3) Line breaks in HTML
$html_output = "Good news! The item# $item_number on which you placed a bid of <br />$ $bid_price is now available for purchase at your bid price.<br />The seller, $bid_user is making this offer.<br /><br />Item Title : $title<br /><br />All the best,<br /> $bid_user<br />$email<br />";
There will be line breaks in your browser and variables will be replaced with their content.
if you are outputting the code as html - change /n -->
and do echo $message;
When we insert any line break with a programming language the char code for this is "\n". php does output that but html can't display that due to htmls line break is . so easy way to do this job is replacing all the "\n" with "". so the code should be
str_replace("\n","<br/>",$str);
after adding this code you wont have to use pre tag for all the output oparation.
copyed this ans from this website :
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 string which I retrieve from a database, I want to calculate the length of the string without spaces but it is displaying a larger value of length(21 characters greater than the actual count) I have removed tab and newline characters and also the php and html tags but no result! I have tried almost every function on the w3schools php reference but I'm unable to find any success. I also have observed that if I don't retrieve the value from the database and input it like this:
$string = "my string";
I get the correct length, please help me. Here is the code:
if($res_tutor[0]['tutor_experience']){
$str = trim(strip_tags($res_tutor[0]['tutor_experience']));
$str = $this->real_string($str);
$space = substr_count($str, ' ');
$experience = strlen($str) - $space;
function real_string($str)
{
$search = array("\t","\n","\r\n","\0","\v");
$replace = array('','','','','');
$str = str_replace($search,$replace,$str);
return $str;
}
And this is the string from the database but as you can see above I have removed all php and html tags using strip_tags() :
<span class=\"experience_font\">You are encouraged to write a short description of yourself, teaching experience and teaching method. You may use the guidelines below to assist you in your writing.<br />
<br />
.Years of teaching experience<br />
.Total number of students taught<br />
.Levels & subjects that you have taught<br />
.The improvements that your students have made<br />
.Other achievements/experience (Relief teaching, a tutor in a tuition centre, Dean's list, scholarship, public speaking etc.)<br />
.For Music (Gigs at Esplanade, Your performances in various locations etc.)</span><br />
</p>
and when I print it, it displays as:
<span class=\"experience_font\">You are encouraged to write a short description of yourself, teaching experience and teaching method. You may use the guidelines below to assist you in your writing.<br />
<br />
.Years of teaching experience<br />
.Total number of students taught<br />
.Levels & subjects that you have taught<br />
.The improvements that your students have made<br />
.Other achievements/experience (Relief teaching, a tutor in a tuition centre, Dean's list, scholarship, public speaking etc.)<br />
.For Music (Gigs at Esplanade, Your performances in various locations etc.)</span><br />
</p>
#Svetilo, not to be rude just wanted to post my findings, your str_replace worked wonderfully, except for the fact that I was still outputting incorrect values with it in the order that you currently have, I found that the following worked flawlessly.
$string = str_replace(array("\t","\r\n","\n","\0","\v"," "),'', $string);
mb_strlen($string, "UTF-8");
Changing around the \r\n & \n made the str_replace not strip out the \n from the \r\n leaving it just a \r.
Cheers.
Try using mb_strlen. http://php.net/manual/en/function.mb-strlen.php
Its more more precise.
mb_strlen($str,"UTF-8")
Where UTF-8 is your default encoding...
To remove all freespaces try something like that..
$string = str_replace(array("\t","\n","\r\n","\0","\v"," "),"",$string);
mb_strlen($string, "UTF-8");
I'm almost there!
Here is the string I am trying to adjust, and my preg_replace attempt.
$description_string = '<p>Here is the test link: “Man or Muppet” with other text afterwards.</p>';
$description = preg_replace( '/(<a[^>]+youtube[^>]*>)+[^"]*(<\/a>)+/', '$0Watch This Video$2', $description );
The result I'm getting is incorrect:
Here is the test link: “Man or Muppet”Watch This Video with other text afterwards.
Any help would be greatly appreciated! THanks!
Well, not sure if you are trying to get the actual title in there. But here is what I came up with:
<?php
$description_string = '<p>Here is the test link: \'Man or Muppet\' with other text afterwards.</p>';
$description = preg_replace( '/(<a[^>]+youtube[^>]*>)+[^"]*(<\/a>)+/', '$1Watch This Video$2', $description_string );
echo $description;
?>
Result:
<p>Here is the test link: Watch This Video with other text afterwards.</p>
Your biggest issue was with the quotes (") being in the title. It is cutting off the anchor tags. While using $0 is also incorrect. You'll need to use $1.
This may not be exactly what you need, but its a quick monkey patch for you.
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
I have the following code:
$message = "Good news! The item# $item_number on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\n
The seller, $bid_user is making this offer.\n
\nItem Title : $title\n
\nAll the best,\n
$bid_user\n
$email\n
";
echo $message;
$htmlContent = $baseClass->email_friend($item_number, $title, $bid_price, $bid_user, $mcat, $message, $email, $VIEWSTATE, $EVENTVALIDATION, $URL);
The problem is that the new line break (\n) is not working.
Try \r\n in place of \n
The difference between \n and \r\n
It should be noted that this is applicable to line returns in emails. For other scenarios, please refer to rokjarc's answer.
I know this is an old question but anyway it might help someone.
I tend to use PHP_EOL for this purposes (due to cross-platform compatibility).
echo "line 1".PHP_EOL."line 2".PHP_EOL;
If you're planning to show the result in a browser then you have to use "<br>".
EDIT: since your exact question is about emails, things are a bit different.
For pure text emails see Brendan Bullen's accepted answer. For HTML emails
you simply use HTML formatting.
Are you building this string using single or double quotes? \r and \n only work with double quotes, as well as embedded variables. For example:
$foo = 'bar';
echo 'Hello \n $foo!';
will output:
Hello \n $foo!
But:
$foo = 'bar';
echo "Hello \n $foo!";
will output:
Hello
bar!
you can use "Line1<br>Line2"
If you output to html or an html e-mail you will need to use <br> or <br /> instead of \n.
If it's just a text e-mail: Are you perhaps using ' instead of "? Although then your values would not be inserted either...
EDIT: Maybe your class for sending emails has an option for HTML emails and then you can use <br />
1) Double-quotes
$output = "Good news! The item# $item_number on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\nThe seller, $bid_user is making this offer.\n\nItem Title : $title\n\nAll the best,\n $bid_user\n$email\n";
If you use double-quotes then \n will work (there will be no newline in browser but see the source code in your browser - the \n characters will be replaced for newlines)
2) Single quotes doesn't have the effect as the double-quotes above:
$output = 'Good news! The item# $item_number on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\nThe seller, $bid_user is making this offer.\n\nItem Title : $title\n\nAll the best,\n $bid_user\n$email\n';
all characters will be printed as is (even variables!)
3) Line breaks in HTML
$html_output = "Good news! The item# $item_number on which you placed a bid of <br />$ $bid_price is now available for purchase at your bid price.<br />The seller, $bid_user is making this offer.<br /><br />Item Title : $title<br /><br />All the best,<br /> $bid_user<br />$email<br />";
There will be line breaks in your browser and variables will be replaced with their content.
if you are outputting the code as html - change /n -->
and do echo $message;
When we insert any line break with a programming language the char code for this is "\n". php does output that but html can't display that due to htmls line break is . so easy way to do this job is replacing all the "\n" with "". so the code should be
str_replace("\n","<br/>",$str);
after adding this code you wont have to use pre tag for all the output oparation.
copyed this ans from this website :