function send_mails($adress, $subject, $message, &$tpl)
{
// fix for correct display of newlines and spaces in the message
$textmail = ereg_replace( "\n", "\r\n", $message);
$textmail = wordwrap($textmail, 70, "\r\n");
//prepare text to show online (html)
$textshow = ereg_replace( "\r\n", "<br>", $textmail);
$sent = '-- HTML Code to show the Mail you sent --';
$tpl->assign(BODY, $sent);
$mailtext = utf8_decode($textmail);
$headers = '-- Functional Header --';
mail ($adress, $subject, $mailtext, $headers);
}
My problem now is that if the message contains apostrophes they're show like this:
We\'re too good, so you shouldn\'t judge this example text\'s content
I've tried $textmail = ereg_replace(" \' ", " ' ", $textmail); but that didn't seem to work.
I'm pretty sure there is an easy fix, but I've been looking for some time now and haven't found a solution... Probably searching for the wrong thing.
string stripslashes ( string $str ) is what you are looking for!
$str = "We\'re too good, so you shouldn't judge this example text\'s content";
$newStr = stripslashes ( $str );
echo $newStr;
Output:
We're too good, so you shouldn't judge this example text's content
EDIT
VolkerK's comment:
I think it might be useful to first determine
whether this would fight the cause or just a symptom...
Please consider, that this is kind of Hot-Fix, you should try to find out, how and where this is actually happening?
Does $message at the beginning contain these slashes? (just echo it.)
If yes, where does $message come from?
If not, go through it all the way $message-> $textmail -> $textshow … and find out, which step does it and eliminate it there!
Related
I need help with a regex, that I'm struggling with.
I'd like to convert:
9.15-11.15
to:
9:15-11:15
I have gotten as far as:
$message = preg_replace('/([0-9]{1,2}+)\.([0-9]{1,2}+)\-([0-9]{1,2}+)\.([0-9]{1,2}+)/', '$0:$1-$3:$4', $message);
This returns "9.15-11.15:9-11:15" though.
I'm not 100% sure if the issue is the match I'm making or my use of preg_replace?
echo str_replace('.', ':', '9.15-11.15' );
enjoy
If you do need a regex for that:
$message = 'Hello. This is some string with 9.15-11.15 time inside, and I don\'t want to replace the DOTS... inside';
$message = preg_replace('/(\d{1,2})\.(\d{1,2})\-(\d{1,2})\.(\d{1,2})/','$1:$2-$3:$4', $message);
var_dump($message);
// string(102) "Hello. This is some string with 9:15-11:15 time inside, and I don't want to replace the DOTS... inside"
"\n" and "\r\n", tested in text message sent by telegram bot, to create line break. Instead of showing line break, underline _ will appear after using them.
How I could printing line feed in telegram message sent by bot?
CODE
$txt = 'با تشکر از عضویت شما، هر روز حدود ساعت 10 شب یک ویدئوی جالب برای شما ارسال خواهد شد.';
$txt .= " \n ";
$txt .= 'Thanks for joining, Every day at almost 18:30 GMT an intersting video will be sent';
Message Demo
Any help will be appreciated.
There is a better way! The problem is because of URL encodings...
You can use normal PHP text using \n but by passing it to urlencode method, as follows:
$txt = urlencode("here is my text.\n and this is a new line \n another new line");
It works for me!
1) If you develop your code in Windows/Linux OS, you can simply use enter in text:
$text = 'test 123
another text';
Thats all!
2) If your code run on Windows/Linux server, you can use PHP_EOL constant instead of \n:
$text = 'text 123 '.PHP_EOL.'yet another text';
3) And if you search for an OS independent soloution, you can use %0A or chr(10) for this purpose:
$text = 'text 123 '.chr(10).'yet another text';
For future visitor just I quote #Dagon answer in comments:
Using %0A will make line feed in telegram messages
You can use %0A instead of \n.
After reading and trying all of these answers, I just wanted to post my own solution. I have an application in Laravel 5.8 that sends the reservation both by e-mail and a Telegram message.
$telegramMessage =
"<strong>Reservation Request</strong>\n".
'<strong>Name:</strong> ' . $reservation->reserv_name . "\n".
'<strong>E-mail:</strong> ' . $reservation->email . "\n".
'<strong>Phone:</strong> ' . $reservation->phone . "\n".
'<strong>Reservation Date/Time:</strong> ' . $reservation->reserv_date_time->format('d-m-Y H:i') . "\n".
'<strong>Number of people:</strong> ' . $reservation->number_of_people . "\n".
'<strong>Message:</strong> ' . $reservation->reserv_message . "\n";
Telegram::sendMessage([
'chat_id' => env('TELEGRAM_CHAT_ID', ''),
'parse_mode' => 'HTML',
'text' => $telegramMessage,
]);
More or less I have used all the html tags that Telegram API allows. You should pay attention \n must be in double quotes.
it may be not show result as you wants in Unicode languages like Persian!
you can prepare your text and use this:
$txt = implode("\n", explode('\n', $txt));
To avoid coding and encoding issues, I used this simple solution in my code:
First, I send my text message in HTML format by setting the parse_mode=HTML argument in the "sendMessage" URL.
Then, I insert the following code for each line break:
<pre>\n</pre>
ie.
... sendMessage?parse_mode=HTML&text="... paragraph1<pre>\n</pre>paragraph2 ..."
Of course the text variable was curl escaped before appended to the URL:
$text = curl_escape($handle, $text);
for me this solution works:
use double quotation mark
$message='Hi'
$message=$message."\n";
$message=$message.'Guys'
All these answers are at the same time "right" and "wrong". In fact in depend a lot of the input you have. For example if you have a text area for input and then send the content to Telegram, if the user write in the text area and press return, the text in Telegram will be
hello\neverybody
and not
hello
everybody
Performing URL encode will change nothing. After struggling a lot I discover a conflict with the fact sending the text area data from a page to another page escape some data.
The way I solve that is to remplace the escaped \n by a non-escaped one.
So:
$my_msg = str_replace("\\n","\n",$my_msg);
It works on Mac, PC and so on with text from text area.
I solved the problem in this way :
$txt = 'aaaaaaaaa\nnew line1 \nnewline2';
$parameters = array('chat_id' => $chatId, "text" => $txt);
$parameters["method"] = "sendMessage";
echo json_encode($parameters);
try here : https://telegram-bot-sdk.readme.io/docs/sendmessage
you should use urlencode to solve this problem:
$text ="sth sth
sth sth";
$text = urlencode($text);
try something like this, its work for me, and you need to add parameter parse_mode.
$text = "exampletest \n example"
or someting like this:
$text1 = 'example';
$text2 = 'next';
$data = $text1 . "\n" . $text2;
https://core.telegram.org/bots/api#formatting-options
It it easy just copy break line from anywhare to pass in code.
enter image description here
ez way😊
$txt = "Thanks for joining, Every day at </br> almost 18:30 GMT an intersting video will be sent";
or
$txt = "Thanks for joining, Every day \r\n at almost 18:30 GMT an intersting video will be sent";
The text of story content in my database is:
I want to add\r\nnew line
(no quote)
When I use:
echo nl2br($story->getStoryContent());
to replace the \r\n with br, it doesn't work. The browser still display \r\n. When I view source, the \r\n is still there and br is nowhere to be found also. This is weird because when I test the function nl2br with simple code like:
echo nl2br("Welcome\r\nThis is my HTML document");
it does work. Would you please tell me why it didn't work? Thank you so much.
The following snippet uses a technique that you may like better, as follows:
<?php
$example = "\n\rSome Kind\r of \nText\n\n";
$replace = array("\r\n", "\n\r", "\r", "\n");
$subs = array("","","","");
$text = str_replace($replace, $subs, $example );
var_dump($text); // "Some Kind of Text"
Live demo here
I doubt that you need "\n\r" but I left it in just in case you feel it is really necessary.
This works by having an array of line termination strings to be replaced with an empty string in each case.
I found the answer is pretty simple. I simply use
$text = $this->storyContent;
$text = str_replace("\\r\\n","<br>",$text);
$text = str_replace("\\n\\r","<br>",$text);
$text = str_replace("\\r","<br>",$text);
$text = str_replace("\\n","<br>",$text);
I'm trying to scrape a website using some regex. But the site isn't written in well formatted html. In fact, the html is horrible and not structured hardly at all. But I've managed to tackle most of it. The problem I'm encountering now is that in some emails, a span is wrapped around a random part of the email like so:
****.*******#g<span class="tournamenttext">mail.com</span>
************<span class="tournamenttext">#yahoo.com</span>
<span class="tournamenttext">**********#mail.com</span>
*******#gmail.com
Is there a way to retrieve the emails with all this inconsistency?
$string ='****.*******#g<span class="tournamenttext">mail.com</span>
************<span class="tournamenttext">#yahoo.com</span>
<span class="tournamenttext">**********#mail.com</span>
*******#gmail.com';
$pattern = "/<\/?span[^>]*>/";
$string = preg_replace($pattern, "", $string);
after that $string will be only mails
****.*******#gmail.com
************#yahoo.com
**********#mail.com
*******#gmail.com
Your code will be like this
$text[1]->innertext = "Where innertext contains something like: "<em>Local (Open)
Tournament.</em> ****.*******#g<span class="tournamenttext">mail.com</span>"
// Firstly clear spans
$pattern = "/<\/?span[^>]*>/";
$text[1]->innertext = preg_replace($pattern, "", $text[1]->innertext);
// Preg Match mail
$email_regex = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"; // Just an example email match regex
preg_match($email_regex, $text[1]->innertext, $theMatch);
echo '<pre>' . print_r($theMatch, true) . '</pre>';
You could simply remove all span tags by replacing </?span[^>]*> with nothing and try your favourite email address finder on the result.
I'm trying to send with function mail(); rich text containing links ; I'm sending this kind of code...
Please, access Contact to send all these information
throw firebug i can see that link tags was removed , code becoming like this
Please, access <a>Contact</a> to send all these information
I need this script , after banning the person who violated rules , to send email to tell the reason why we banned him .
On another email services email comes without problems , what is my mistake , sorry for my English , down i'll show a part from script for sending email , the important one..
// Set and wordwrap message body
$body = "From: $name\n\n";
$body .= "Message: $message";
$body = wordwrap($body, 70);
// Build header
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= "From: $email\n";
if ($cc == 1) {
$headers .= "Cc: $email\n";
}
$headers .= "X-Mailer: PHP/Contact";
// Send email
if(!#mail($to, $subject, $body, $headers)) {
echo '<b> ERROR ! </b> Unfortunately, a server issue prevented delivery of your message<br />'; }
Unless you are doing something to $body in the code you have not posted here, my guess is that it is wordwrap() that causes the problem. In the php manual is a user-contributed function which might help:
http://www.php.net/manual/en/function.wordwrap.php#89782
Part of the problem is the long lines, but wordwrap() is not sufficient to solve that.
An email could have arbitrarily long lines, but these are transmitted over a protocol which only allows short lines. So long lines have to be split. The protocol tags lines which have been split by adding = to then end of them so what starts out looking like this.
Characters 2 3 4 5 6 7
12346790123456789012345678901234567890132456789012345678901234567890123456789
This is a long line with text which goes beyond the 78 character limit imposed by the protocol
ends up looking like this
This is a long line with text which goes beyond the 78 character limit=
imposed by the protocol
Using = like that though means that = can't be used in your message, so it has to be escaped. So you need to replace = in your message with =3D (where 3D is the hex code for =).
It's also wise to replace any control characters (with ascii code < 32) with =xx and anything with ascii code over 126 too. I use these functions to do this, you then just need to do $message=encodeText($message) before you send and the problem should go away.
function encodeText($input) {
// split input into lines, split by \r\n, \r or \n
$lines=preg_split("/(?:\r\n|\r|\n)/", $input);
$text="";
// for each line, encode it into a 78 char max line.
for ($i=0; $i<count($lines); $i++) {
$text.=encodeLine($lines[$i]);
}
return $text;
}
/**
* This splits a line into a number of pieces, each shorter than the 78 char
* limit. It also add continuation marks (i.e. =) to then end of each piece
* of the resulting line, and escapes any = characters, control characters
* or characters with bit 8 set, and backspace.
* #return a multiline string (with /r/n linebreaks)
*/
function encodeLine($line) {
$split=Array();
$result="";
$piece='';
$j=0;
for ($i=0; $i<strlen($line); $i++) {
// if you've just split a line, you'll need to add an = to the
// end of the previous one
if ($j>0 && $piece=="") $split[$j-1].="=";
// replace = and not printable ascii characters with =XX
if (ord($line{$i})==0x3D) {
$piece.="=3D";
} else if (ord($line{$i})<32) {
$piece.="=".bin2hex($line{$i});
} else if (ord($line{$i})>126) {
$piece.="=".bin2hex($line{$i});
} else {
$piece.=$line{$i};
}
// if the resulting line is longer than 71 characters split the line
if (strlen($piece)>=72) {
$split[$j]=$piece;
$piece="";
$j++;
}
}
// the last piece being assembled should be added to the array of pieces
if (strlen($piece)>0) $split[]=$piece;
// if a piece ends in whitespace then replace that whitespace with =20
// for a space or =09 for a tab.
for ($i=0; $i<count($split); $i++) {
$last=substr($split[$i],-1);
if ($last=="\t") $last="=09";
if ($last==" ") $last="=20";
$split[$i]=substr($split[$i],0,strlen($split[$i])-1).$last;
}
// assemble pieces into a single multiline string.
for ($i=0; $i<count($split); $i++) {
$result.=$split[$i]."\r\n";
}
return $result;
}
This might be to late but, oh well just figure it out. At least that's what I'm seeing here.
Basically I'm generating the Newsletter dynamic based on some data and when I found inside a string a specific syntax I had to replace it with a anchor tag. Anyway, here it is:
Bad formatting:
"<a href='" + url + "'>" + name + "</a>";
Good formatting:
'' + name + '';
Straight answer, use double-quotes for href attribute.
Try this out , Use stripslashes() with the email body and it should worked perfectly fine
$body= stripslashes($mail_message);