Convert inline specified UTF-8 mail subject to UTF-8 text - php

I want to convert the following raw mail subject to normal UTF-8 text:
=?UTF-8?B?UmU6ICMyLUZpbmFsIEFjY2VwdGFuY2UgdGVzdCB3aXRoIG5ldyB0ZXh0IHdpdGggU2xvdg==?=
=?UTF-8?B?YWsgaW50ZXJwdW5jdGlvbnMgIivEvsWhxI3FpcW+w73DocOtw6khxYgi?=
The real text for that is (yes, there are some crazy diacritics in it):
Re: #2-Final Acceptance test with new text with Slovak interpunctions "+ľščťžýáíé!ň"
The way I handle mail subjects:
function subject_imapUtf8($str){
$conv = '';
$subParts = preg_split('/[\r\n]+/',$str);
for($i=0;$i<count($subParts);$i++){
$conv .= imap_utf8(trim($subParts[$i]));
}
return $conv;
}
For the example this gives me:
=?UTF-8?B?UmU6ICMyLUZpbmFsIEFjY2VwdGFuY2UgdGVzdCB3aXRoIG5ldyB0ZXh0IHdpdGggU2xvdg==?=ak interpunctions "+ľščťžýáíé!ň"
So as you can see the second part/line of the subject is converted correclty.
What do I need to change to convert the first part correctly?

mb_internal_encoding("UTF-8");
echo mb_decode_mimeheader($mime);
Demo http://codepad.viper-7.com/a9l4IA

Related

How to get imap body text with german locale in php?

Facing the issue that when I try to get the body of an email through imap in php, the content of the variable $body is
Jääätzt Kööönmän umläute mit ÄnhÄÄÄng
instead of
Jääätzt Kööönmän umläute mit ÄnhÄÄÄng.
I do not understand where the problem is, all other characters like ?,!,$ are working fine, just german characters like ä,ö are making those problems.
The code-block in php I am using is
$body = imap_fetchbody($mbox, $overview->msgno, 1.2);
if (!strlen($body) > 0) {
$body = imap_fetchbody($mbox, $overview->msgno, 1);
}
Do you have any ideas? I am really desperate.
The Mail you try to show is not in the same encoding as the headers of your website. That's why your browser can't show the special chars properly, like the Umlaut in this case.
You can for example convert them to utf-8 with imap_utf8
$body = imap_fetchbody($mbox, $overview->msgno, 1.2);
if (!strlen($body) > 0) {
$body = imap_fetchbody($mbox, $overview->msgno, 1);
}
$body = imap_utf8($body);
http://php.net/manual/de/function.imap-utf8.php

Special characters to HTML ASCII Entity Equivalent

How will I convert all special characters to their corresponding html entity?
Special character would be like $ & / \ { } ( - ' , # etc.
I tried to use htmlentities() and htmlspecialchars(). But didn't solve my problem.
please check here. I want output like Entity Number i.e. column 3.
Actually the scenario is - I need to take input from fckeditor. and then save into the database. So I need to convert all special character to their corresponding html entity, from the text. Otherwise it's giving me error.
What you are looking is for an ASCII equivalent of a character. So you need to make use of ord().
By the way what divaka mentioned is right.
Do like this..
<?php
function getHTMLASCIIEquiv($val)
{
$arr=['$','&','/','\\','{','}','(','-','\'',',','#'];
$val = str_split($val);$str="";
foreach($val as $v)
{
if(in_array($v,$arr))
{
$str.="&#".ord($v).";";
}
else
{
$str.=$v;
}
}
return $str;
}
echo getHTMLASCIIEquiv('please check $100 & get email from test#cc.com');
OUTPUT :
please check $100 & get email from test#cc.com
Demo

Some Japanese characters not displaying correctly

I'm having a problem where I'm trying to parse an email, and then post the email content to a website. The email may contain Japanese or English. The Japanese displays 99% correctly on the website, but every now and then a character will be swapped for another, or it will display as garbage.
Here's the code being used to get the proper encoding for the email body-
$post->content = quoted_printable_decode($parser->getMessageBody('text'));
$isISO2022 = $parser->isISO2022();
$post->content = ($isISO2022)
? mb_convert_encoding($post->content, 'UTF-8', 'iso-2022-jp')
: mb_convert_encoding($post->content, 'UTF-8', mb_detect_encoding($post->content));
$post->save();
The parser's isISO2022 function:
public function isISO2022() {
$isISO2022 = false;
foreach ($this->parts as $part) {
if (isset($part['headers']['content-type']) && preg_match('/iso-2022-jp/i',$part['headers']['content-type'])) {
$isISO2022 = true;
}
}
return $isISO2022;
}
Anyone have any ideas what's going on?
Added:
I have heard that there are some specific characters that are not supported by iso-2022-jp, and you should use iso-2022-jp-ms instead, but when I try to use iso-2022-jp-ms, it says invalid encoding. It also seems to me that the characters I've seen it not display correctly are basic characters, and should be universally supported.

PHP json_encode() making data null

I have this code here:
case 'resource_list':
if(file_exists('content.php')){
include('../ajax/content.php');
} else {
die('does not exist');
}
$html = render_content_page($array[1],$array[2]);
$slate = 'info_slate';
$reply_array = array(
'html' => $html,
'slate' => $slate
);
echo json_encode($reply_array);
break;
i have debugged every level right up until json_encode() is called. But the data i receive back in my ajax is nul for the html key. This code is essentially a copy and paste of another case the just calls a function other than render_content_page() but that works perfectly fine.
$reply_array var_exports to:
array (
'html' => '<ol>
<li unit="quiz" identifier=""><img src="img/header/notifications.png"/>Fran�ois Vase Volute Krater</li>
</ol>',
'slate' => 'info_slate',
)
My initial thought is that special character in Fran�ois Vase Volute Krater, as json_encode only works with UTF-8 encoded data.
Try UTF-8 encoding it before JSON encoding it like so:
json_encode(utf8_encode("Fran�ois Vase Volute Krater"));
Maybe problem is with encoding?
As manual states, json_encode() works only only with utf8 encoded data:
This function only works with UTF-8 encoded data.
http://php.net/json_encode
As documented, json_encode expects its input text in UTF-8. Most likely, your input (the ç) is not in UTF-8.
Use utf8_encode (if you're currently using ISO-8859-1) or mb_convert_encoding (otherwise) to convert input strings to UTF-8.

Problem in converting string to puny code (in PHP, using phlyLabs's punycode string converter)

i'm using the code from here: http://phlymail.com/en/downloads/idna/download/ and built a function like this (from the example):
function convert_to_punycode($inputstring)
{
$IDN = new idna_convert();
// The input string, if input is not UTF-8 or UCS-4, it must be converted before
$inputstringutf8 = utf8_encode($inputstring);
// Encode it to its punycode presentation
$outputstringpunycode = $IDN->encode($inputstringutf8);
return $outputstringpunycode;
}
However it doesnt work properly.
For the input: Россию
It gives: РоÑÑиÑ
Whereas it should give: xn--h1alffa3f
What am I doing wrong? $inputstring which is being passed is a normal string with no special declarations/etc...
Is your string already UTF-8? Looks like it.
Or is it in ISO-8859-5?
In both cases you cannot use the PHP function utf8_encode(), since it expects your input string to be ISO-88591-1 (ISO Latin-1, Western European languages). Look into the file transcode_wrapper.php, which is delivered with the class source. This might help you.
you might need PHP IDNA Extension
I'd just add something like to use if possible the module otherwise Dave suggested function :
if(!function_exists('idn_to_ascii') and !function_exists('idn_to_utf8'))
{ define('IDN_FALLBACK_VERSION',2008);
require_once('idna_convert.class.php');
function idn_to_ascii($string)
{ $IDN = new idna_convert(array('idn_version'=>IDN_FALLBACK_VERSION));
return $IDN->encode($string);
}
function idn_to_utf8($string)
{ $IDN = new idna_convert(array('idn_version'=>IDN_FALLBACK_VERSION));
return $IDN->decode($string);
}
function idn_to_unicode($string){return idn_to_utf8($string);}
}
Try this method to convert encoding
//$inputstringutf8 = utf8_encode($inputstring);
$inputstringutf8 = mb_convert_encoding($inputstring, 'utf-8', mb_detect_encoding($inputstring));

Categories