here is my example text
------=_Part_564200_22135560.1319730112436
Content-Type: text/plain; charset=utf-8; name=text_0.txt
Content-Transfer-Encoding: 7bit
Content-ID: <314>
Content-Location: text_0.txt
Content-Disposition: inline
I hate u
------=_Part_564200_22135560.1319730112436
Content-Type: image/gif; name=dottedline350.gif
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=dottedline350.gif
Content-ID: <dottedline350.gif>
I need to be able to extract the "I hate u".
I know i can use explode to remove the last part after the message by doing
$body_content = garbled crap above;
$message_short = explode("------=_",$body_content);
echo $message_short;
but i need to remove the first part, along with the last part.
So, explode function above does what I need to remove the end
now i need something that says
FIND 'Content-Disposition: inline' and remove along with anything before
then
FIND '------=_' and remove along with anything after
then
Anything remaining = $message_short
echo $message_short;
will look like
I hate u
Any help would be appreciated.
Thanks #Marcus
This is the code i now have and works wonderfully.
$body_content2 = imap_qprint(imap_body($gminbox, $email_number));
$body_content2 = strip_tags ($body_content2);
$tmobile_body = explode("------=_", $body_content2);
$tmobile_body_short = explode("Content-Disposition: inline", $tmobile_body[1]);
$tmobile_body_complete1 = trim($tmobile_body_short[1]);
$tmobile_body_complete2 = explode("T-Mobile", $tmobile_body_complete1);
$tmobile_body_complete3 = trim($tmobile_body_complete2[1]);
If you want to do it with explode and explode on "Content-Disposition: inline" here's how you can do:
$message_short = explode("------=_", $body_content);
$message_shorter = explode("Content-Disposition: inline", $message_short[1]);
$content = trim($message_shorter[1]);
// $content == I hate you
Note that this demands that the last line is Content-Disposition: inline
If you want to solve it using regex and use the double linebreak as a delimiter here's a codesnippet for you:
$pattern = '/(?<=------=_Part_564200_22135560.1319730112436)(?:.*?\s\s\s)(.*?)(?=------=_Part_564200_22135560.1319730112436)/is';
preg_match_all($pattern, $body_content, $matches);
echo($matches[1][0]);
Output:
I hate you
Here's a link to codepad with sample text.
Related
I have the following text in a string called $test:
Content-Type: text/plain
Server: testapp (4.2.1 (x86_64/linux))
Content-Length: 125
{"password":"123","email_address":"","name":"j.doe","username":"jd123"}
I am trying to write a regular expression in php that will return everything after content-length: 125.
Here's what I have so far:
if (preg_match('/^Content\-Length\:[0-9\\n]+([a-zA-Z0-9\{\}\"\:])*/',$test,$result))
{
var_dump($result[1]);
}
I don't get any error messages, but it doesn't find the pattern I've defined in my string.
I've also tried this pattern:
'/^Content\-Length\:[0-9\\n]+([a-zA-Z0-9{}\"\:])*/'
where I tried to remove the escape char infront of the curly braces. But it's still a no go.
Can you tell me what I'm missing?
Thanks.
EDIT 1
my code now looks like this:
<?php
$test = "Content-Type: text/plain
Server: kamailio (4.2.1 (x86_64/linux))
Content-Length: 125
{"password":"test123","email_address":"","name":"j.doe","username":"jd123"}";
//if (preg_match('/Content-Length\:[0-9\\n]*([a-zA-Z0-9{}\"\:])*/',$test,$result))
//{
// var_dump($result);
//}
preg_match('/({.*})/', $str, $matches);
echo $matches[0];
?>
That gives me the following error:
Undefined offset: 0 in /var/www/html/test/test.php on line 31
Line 31 is where I'm trying to echo the matches.
$str = <<<HEREDOC
Content-Type: text/plain
Server: testapp (4.2.1 (x86_64/linux))
Content-Length: 125
{"password":"123","email_address":"","name":"j.doe","username":"jd123"}
HEREDOC;
preg_match('/(\{.*\})/', $str, $matches);
echo $matches[0];
The regex here is simply matching a line that begins with { and ends with }. It's a quick and loose regex, however.
Instead of using a big pattern to match everything (which is timeconsuming) - why not use preg_split to cut your string into two pieces at your desired location?
$string = 'Content-Type: text/plain
Server: testapp (4.2.1 (x86_64/linux))
Content-Length: 125
{"password":"123","email_address":"","name":"j.doe","username":"jd123"}';
$parts = preg_split ("/Content-Length:\s*\d+\s*/", $string);
echo "The string i want is '" . $parts[1] . "'";
Output:
The string i want is '{"password":"123","email_address":"","name":"j.doe","username":"jd123"}'
You can avoid the regex altogether because the HTTP header is always separated from the response body by 2 consecutives line breaks.
list($headers, $body) = explode("\n\n", $string);
Or for windows-style breaks( which by the way are the standard for HTTP headers):
list($headers, $body) = explode("\r\n\r\n", $string);
I am using PHP mail() function:
$to = 'AAAA <postmaster#xxx.xx>';
$subject = 'BBBB';
$message = "CCCC\r\nCCCC CCCC \r CCC \n CCC \r\n CCC \n\r CCCC";
$headers = 'From: DDD<postmaster#xxx.xx>' . "\r\n";
$headers .= "Content-Type: text/html; charset=\"UTF-8\"; format=flowed \r\n";
$headers .= "Mime-Version: 1.0 \r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable \r\n";
mail($to, $subject, $message, $headers);
When I receive this email it looks like this:
CCCC CCCC CCCC CCC CCC CCC CCCC
I would expect something like this:
CCCC
CCCC CCCC CCC
CCC
CCC
CCCC
It works fine without Content-Type HTTP header. How can I make new lines and still use my "Content-Type" declaration?
You need to use a <br> because your Content-Type is text/html.
It works without the Content-Type header because then your e-mail will be interpreted as plain text. If you really want to use \n you should use Content-Type: text/plain but then you'll lose any markup.
Also check out similar question here.
If you are sending HTML email then use <BR> (or <BR />, or </BR>) as stated.
If you are sending a plain text email then use %0D%0A
\r = %0D (Ctrl+M = carriage return)
\n = %0A (Ctrl+A = line feed)
If you have an email link in your email,
EG
Send email
Then use %250D%250A
%25 = %
You need to use <br> instead of \r\n . For this you can use built in function call nl2br
So your code should be like this
$message = nl2br("CCCC\r\nCCCC CCCC \r CCC \n CCC \r\n CCC \n\r CCCC");
If you use content-type: text/html you need to put a <br> because your message will be threated like an html file.
But if you change your content-type to text/plain instead of text/html you will be able to use \r\n characters.
Using
<BR>
is not allways enough. MS Outlook 2007 will ignore this if you dont tell outlook that it is a selfclosing html tag by using
<BR />
You can add new line character in text/plain content type using %0A character code.
For example:
<a href="mailto:someone#example.com?subject=Hello%20again&body=HI%20%0AThis%20is%20a%20new%20line"/>
Here is the jsfiddle
This worked for me.
$message = nl2br("
===============================\r\n
www.domain.com \r\n
===============================\r\n
From: ".$from."\r\n
To: ".$to."\r\n
Subject: ".$subject."\r\n
Message: ".$_POST['form-message']);
' '
space was missing in my case, when a blank space added ' \r\n' started to work
Another thing use "", there is a difference between "\r\n" and '\r\n'.
"\n\r" produces 2 new lines while "\n","\r" & "\r\n" produce single lines if, in the Header, you use content-type: text/plain.
Beware: If you do the Following php code:
$message='ab<br>cd<br>e<br>f';
print $message.'<br><br>';
$message=str_replace('<br>',"\r\n",$message);
print $message;
you get the following in the Windows browser:
ab
cd
e
f
ab cd e f
and with content-type: text/plain you get the following in an email output;
ab
cd
e
f
for text/plain text mail in a mail function definitely use PHP_EOL constant, you can combine it with too for text/html text:
$messagePLAINTEXT="This is my message."
. PHP_EOL .
"This is a new line in plain text";
$messageHTML="This is my message."
. PHP_EOL . "<br/>" .
"This is a new line in html text, check line break in code view";
$messageHTML="This is my message."
. "<br/>" .
"This is a new line in html text, no line break in code view";
OP's problem was related with HTML coding. But if you are using plain text, please use "\n" and not "\r\n".
My personal use case: using mailx mailer, simply replacing "\r\n" into "\n" fixed my issue, related with wrong automatic Content-Type setting.
Wrong header:
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Correct header:
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
I'm not saying that "application/octet-stream" and "base64" are always wrong/unwanted, but they where in my case.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->isHTML(true);
Insert this code after working
all html tag like
<br> <p> in $mail->Body='Hello<br> how are you ?<b>';
I need to remove all dodgy html characters from a web-site I'm parsing using Curl and simplehtml dom.
<?php
$html = "this is a text";
var_dump($html);
var_dump(html_entity_decode($html,ENT_COMPAT,"UTF-8"));
Which outputs
string(19) "this is a text"
string(15) "this is a text"
I don't want to use preg* as there are other characters in the text (e.g. °).
This is driving me insane now!
Thanks,
James
You need to specify your output encoding with a header:
<?php
header('Content-Type: text/html; charset=utf-8');
$html = "this is a text";
var_dump($html);
var_dump(html_entity_decode($html,ENT_COMPAT,"UTF-8"));
?>
The browser does not assume UTF-8 by default, that's why it displays the wrong character.
If that's the only character that needs replacing just use str_replace()
var_dump(str_replace(' ', ' ', "this is a text"));
See it in action
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
My Text :
12a49803-713c-4204-a8e6-248e554a352d_ Content-Type: text/plain; charset="iso-8859-6" Content-Transfer-Encoding: base64 DQrn0Ocg0dPH5MkgyszR6sjqySDl5iDH5OfoyuXq5A0KDQrH5OTaySDH5NnRyOrJIOXP2ejlySAx MDAlDQogCQkgCSAgIAkJICA= --_12a49803-713c-4204-a8e6-248e554a352d_ Content-Type: text/html; charset="iso-8859-6" Content-Transfer-Encoding: base64 PGh0bWw+DQo8aGVhZD4NCjxzdHlsZT48IS0tDQouaG1tZXNzYWdlIFANCnsNCm1hcmdpbjowcHg7
I want to extract iso-8859-6
you could do: preg_match('/charset="([^"]+)"/',$string,$m); echo $m[1];
Edit: In case all need matching (prompted from other answer) modify like this:
preg_match_all('/charset="([^"]+)"/',$string,$m); print_r($m);
The regex you are looking for is:
iso[^"]+
The php code you need is:
<?php
$subject='12a49803-713c-4204-a8e6-248e554a352d_ Content-Type: text/plain; charset="iso-8859-6" Content-Transfer-Encoding: base64 DQrn0Ocg0dPH5MkgyszR6sjqySDl5iDH5OfoyuXq5A0KDQrH5OTaySDH5NnRyOrJIOXP2ejlySAx MDAlDQogCQkgCSAgIAkJICA= --_12a49803-713c-4204-a8e6-248e554a352d_ Content-Type: text/html; charset="iso-8859-6" Content-Transfer-Encoding: base64 PGh0bWw+DQo8aGVhZD4NCjxzdHlsZT48IS0tDQouaG1tZXNzYWdlIFANCnsNCm1hcmdpbjowcHg7';
$pattern='/iso[^"]+/m';
if (preg_match($pattern, $subject, $match))
echo $match[0];
?>
The output is:
iso-8859-6
if you are interested in getting both matches (since you have 2 in the string) and iterate through them you should do something like this.
also i used single quotes to not have to escape the quotes inside the regex. used ridgerunners suggestions aswell.
preg_match_all('/charset="([^"]+)"/', $subject, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[0]); $i++) {
# Matched text = $result[0][$i];
}
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);